Creates a JavaScript object from XML.
Note: If the XML element is namespace aware and has a namespace prefix, use quotes notation to access it.
Defines the XML string to convert to a JavaScript object.
(Optional) Returns the element that has the specified tag name. If you do not provide the elementTagName, the method returns the root element.
Returns a value of Object type.
Consider the following XML to convert it to a JavaScript object:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer”s Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
This example converts the preceding XML data to a JavaScript object.
var parsedXML = ca_pam_convertXMLToJSObject(xmlString);
var bookObj = parsedXML . book[0]; var bookObj = parsedXML [“book”][0];
var idVal = bookObj.id; var idVal = bookObj[“id”];
var bookDesription = bookObj. description[0]._text; var bookDesription = bookObj[“description”][0][“_text”];
Note: If an element has an attribute and an element with the same name, access them as an array. In the array, the first member (index 0) is always the attribute and the elements follow.
Consider the following XML to convert it to a JavaScript object:
<?xml version="1.0"?>
<categories>
<category name="Weather">
<name part="1">Sunny</name>
<name part="2">Rainy</name>
</category>
<category name="Climate">
<name part="1">Wet</name>
<name part="2">Dry</name>
</category>
</categories>
This example converts the preceding XML data to a JavaScript object.
var parsedXML= ca_pam_convertXMLToJSObject(xmlString); var firstCatagory= parsedXML.category[0];
var bookName = firstCatagory.name[0];
var firstBookName = firstCatagory.name[1]._text;
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <Header/> <Body> <getStartRequestFormsResponse xmlns="http://www.ca.com/itpam"> <startRequests> <startRequest name="RahulSrf" refPath="/Folder/"> <description/> </startRequest> <startRequest name="Start Request Form" refPath="/RestSupport/"> <description/> </startRequest> </startRequests> </getStartRequestFormsResponse> </Body> </Envelope>
This example shows the usage of the ca_pam_convertXMLToJSObject(xmlString, elementTagName) method with the elementTagName parameter.
The following script retrieves all the <startRequest> elements from XML:
var srfResult = ca_pam_convertXMLToJavaScriptObject(xmlString,'startRequest');
srfResult is an array of all the startRequest elements.
<?xml version="1.0"?>
<note _text="abc">
<_text>Tove</_text>
</note>
This example shows a scenario in which the XML has _text as an element or attribute. In this case, the method creates an array with the name "_text".
The following script accesses the values:
var parsedXML= ca_pam_convertXMLToJavaScriptObject(xmlString); var textNode= parsedXML["_text"][0]; var attributeValue= parsedXML["_text"][1]; var childTextNodeValue= parsedXML["_text"][2]["_text"];
The script has the following result:
textNode="";
(...as there is no scalar value with the node.)
attributeValue="abc"; childTextNodeValue="Tove"
You can also find this function in the out-of-the-box content in CA Process Automation.
|
Copyright © 2013 CA.
All rights reserved.
|
|