Working with XML
All XML content converted to the JSON when received from the API. And JSON body is converted to the XML when sending to the API. This allows to work with XML content in the same way as with JSON, using all available options like chaining, generating, etc. Although, because XML cannot be fully represented as a JSON structure, some additional rules must be followed while assembling or reading XML payload through JSON.
Special rules
Use the following rules whenever reading the XML Response or constructing the XML request.
- All tag attributes are represented as a separate key with the
-
prefix. For example, the XML<tag attr="value"></tag>
would be represented as{"tag": {"-attr": "value"}}
. - If tag contains attributes and text, the text would be represented as a separate field with
the
#text
name. For example, the XML<tag attr="value">content</tag>
would be represented as{"tag": {"-attr": "value", "#text": "content"}}
. <?xml version="1.0" encoding="UTF-8"?>
header added automatically to the XML request before sending it to the API.
XML payload in JSON sample
XML
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:p="urn:schemas-books-com:prices">
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<p:price>30.00</p:price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<p:price>29.99</p:price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<p:price>49.99</p:price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<p:price>39.95</p:price>
</book>
</bookstore>
JSON generated from XML
{
"bookstore": {
"-xmlns:p": "urn:schemas-books-com:prices",
"book": [
{
"-category": "COOKING",
"author": "Giada De Laurentiis",
"p:price": "30.00",
"title": {
"#text": "Everyday Italian",
"-lang": "en"
},
"year": "2005"
},
{
"-category": "CHILDREN",
"author": "J K. Rowling",
"p:price": "29.99",
"title": {
"#text": "Harry Potter",
"-lang": "en"
},
"year": "2005"
},
{
"-category": "WEB",
"author": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Vaidyanathan Nagarajan"
],
"p:price": "49.99",
"title": {
"#text": "XQuery Kick Start",
"-lang": "en"
},
"year": "2003"
},
{
"-category": "WEB",
"author": "Erik T. Ray",
"p:price": "39.95",
"title": {
"#text": "Learning XML",
"-lang": "en"
},
"year": "2003"
}
]
}
}
Simple example of sending and reading XML
{{ template "pg-layout" . }}
{{ define "body" }}
<form method="post">
<input type="hidden" name="CustomApi.Api.Post.soapReq" value="https://www.dataaccess.com/webservicesserver/NumberConversion.wso">
<input type="hidden" name="CustomApi.Api.Header.soapReq.Content-Type" value="text/xml; charset=utf-8">
<input type="hidden" name="CustomApi.Api.Xml.soapReq.string.soap:Envelope.-xmlns:soap" value="http://schemas.xmlsoap.org/soap/envelope/">
<input type="hidden" name="CustomApi.Api.Xml.soapReq.stirng.soap:Envelope.soap:Body.NumberToWords.-xmlns" value="http://www.dataaccess.com/webservicesserver/">
<input type="text" name="CustomApi.Api.Xml.soapReq.string.soap:Envelope.soap:Body.NumberToWords.ubiNum" value="">
<button type="submit">Send</button>
</form>
<h2>Sent:</h2>
<pre>{{ .Data.CustomApi.Api.Requests.soapReq.Xml.Raw | jsonPretty }}</pre>
<h2>Received:</h2>
<pre>{{ .Data.CustomApi.Api.Requests.soapReq.Response.Raw | jsonPretty }}</pre>
Requested number as text:
<b>{{ .Data.CustomApi.Api.Requests.soapReq.Response.Get "Envelope.soap:Body.NumberToWordsResponse.m:NumberToWordsResult" }}</b>
{{ end }}