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"?><bookstorexmlns:p="urn:schemas-books-com:prices"><bookcategory="COOKING"><titlelang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><p:price>30.00</p:price></book><bookcategory="CHILDREN"><titlelang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><p:price>29.99</p:price></book><bookcategory="WEB"><titlelang="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><bookcategory="WEB"><titlelang="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" }}
<formmethod="post"><inputtype="hidden"name="CustomApi.Api.Post.soapReq"value="https://www.dataaccess.com/webservicesserver/NumberConversion.wso"><inputtype="hidden"name="CustomApi.Api.Header.soapReq.Content-Type"value="text/xml; charset=utf-8"><inputtype="hidden"name="CustomApi.Api.Xml.soapReq.string.soap:Envelope.-xmlns:soap"value="http://schemas.xmlsoap.org/soap/envelope/"><inputtype="hidden"name="CustomApi.Api.Xml.soapReq.stirng.soap:Envelope.soap:Body.NumberToWords.-xmlns"value="http://www.dataaccess.com/webservicesserver/"><inputtype="text"name="CustomApi.Api.Xml.soapReq.string.soap:Envelope.soap:Body.NumberToWords.ubiNum"value=""><buttontype="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 }}
Working with XML in GoErp
Modifying XML document
xmlInsertChildAt
Inserts a child node at the specified by xPath position in the XML document. Optionally, also can
apply indentation to whole document and allows to define position in the path element where to
insert the child.
Parameters
xml - XML document to modify.
child - Child node to insert.
xPath - XPath expression to find the position where to insert the child node.
position - Optional. If provided, the child node will be inserted at the specified position in the path element.
indent - Optional. If provided, the XML document will be indented by a given amount of spaces.