Frontend XmlParse - QuinnBast/SaskTel-Communication-Portal GitHub Wiki
The XmlParse.js provides some helper functions for analyzing Xml data that is generated from the xmljs library. This library is used because it is one of the only javascript libraries that preserves element order when converting between xml and JSON.
The BroadSoft.jsx file will automatically return data in the format of a JSON object created by the xmljs library. In order to get a specific element within the JSON object that is returned, a small amount of processing needs to be done. In order to make this processing easier and happen behind the scenes, the getTag
function is provided in order to recursively loop the xmljs object in order to find the tag that has been specified. For example, given the following XML data:
<XmlRoot>
<value>false</value>
<Element>
<innerValue>someValue</innerValue>
</Element>
</XmlRoot
Various elements of data could be extracted as follows:
import {getTag} from "XmlParse"
//ResponseData is the Xml shown above parsed with xmljs library
getTag(responseData, ["XmlRoot", "value"]); //Returns false
getTag(responseData, ["XmlRoot", "Element", "innerValue"]); //Returns "someValue"
Arg | Description | Example(s) |
---|---|---|
xml | The xmljs formatted data to parse for a data element. | xmljs.xml2js("<a>hi</a>"); |
XmlLocation | An array of strings which points to the location where the data is located in the Xml response. |
["Root", "value"] ["Root", "Node1", "Node2", "valueTag"]
|
The setTag
function allows setting a specific xml tag within the xmljs formatted data. If the specified tag does not exist, then the tag will be created instead. This function acts similarly to the GetTag function, however it also takes in a parameter to set the value of the tag to. Using the same XML data as above, the following example shows how to set the value of the tags:
import {setTag} from "XmlParse"
//ResponseData is the Xml shown above parsed with xmljs library
setTag(responseData, ["XmlRoot", "value"], true); //Sets the value tag to be true
setTag(responseData, ["XmlRoot", "Element", "innerValue"], "newValue"); //changes "someValue" to "newValue"
setTag(responseData, ["XmlRoot", "Element", "newTag"], "newTagValue"); //Creates a new XmlTag in the response and sets the value of the tag to "newTagValue"
Arg | Description | Example(s) |
---|---|---|
xml | The xmljs formatted data to parse for a data element. | xmljs.xml2js("<a>hi</a>"); |
XmlLocation | An array of strings which points to the location where the data is located in the Xml response. |
["Root", "value"] ["Root", "Node1", "Node2", "valueTag"]
|
tagValue | The value to set the tag to. | true false 123 |
In order to update a value in the API execution server, only the updated tag needs to be sent to the server in order to be updated. Therefore, the following XML response is a valid response to send to update a single tag:
<RootElement>
<updatedValue>newValue</updatedValue>
</RootElement>
This function allows generating a response of this format, which makes updating a value in the XML significantly easier. An example of this function can be seen below.
import {generateXml} from "XmlParse"
// This will return an xmljs object for the XML shown above.
let generatedXml = generateXml(["RootElement", "updatedValue"], "newValue");
Arg | Description | Example(s) |
---|---|---|
xmlStructure | An array of 2 elements indicating the name of the root node, and the updated value. | ["RootName", "valueName"] |
value | The value to set the inner tag to. This should be the newly updated tag value. | "newValue" |
When updating Xml, sometimes just sending the updated value isn't enough. Sometimes, a list of elements exist and a key also needs to be specified in order to link the updated value to a specific entry in a list. This funciton generates an xml response with a key-value pair in order to send an update. In order to generate the following Xml response:
<RootElement>
<Element>
<key>2</key>
<value>newValue</value>
</Element>
</RootElement>
The following code can be used to generate this XML:
import {generateKeyValueXml} from "XmlParse"
// Generates the XML shown above.
generateKeyValueXml("RootElement", "Element", "key", 2, "value", "newValue")
Arg | Description | Example(s) |
---|---|---|
rootName | A string of the name of the root element. | "RootName" |
entryName | A string indicating what the tag for a single element in the list is. | "ListElement" "CallEntry" "Entry" |
keyName | The name of the xmlTag for the key. | "key" |
keyValue | The value of the key that should be updated. | 2 |
valueName | The XmlTag corresponding to the updated value. | "value" |
value | The value of the updated tag. | "newValue" |