Search

parsing Xml Response


// Save all entity nodes in an array. Each result is returned in a BusinessEntity node.
// All BusinessEntity nodes are contained in a single BusinessEntities node.
// The BusinessEntities node in contained in a RetrieveMultipleResult node
// You could also use the XPath //BusinessEntities/BusinessEntity or //BusinessEntity
// "//" tells the XML parser to find all occurrences in the document starting with the
// supplied path, so it would find a/b/c/BusinessEntity as well as x/BusinessEntity.
var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

// Loop through the collection of returned entities.
// Note that the query above limits the result to a single entity, so you will only find one
// node. To be more specific, it could be 0 nodes as well, if you don't have access to accounts
// or your system is empty.
for (var i = 0; i < entityNodes.length; i++) {

// Access the current array element
var entityNode = entityNodes[i];

// Attributes are child nodes of the BusinessEntity node. Use selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used before returns all
// matching nodes, but as an attribute name only occurs once, selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var accountidNode = entityNode.selectSingleNode("q1:accountid");
var nameNode = entityNode.selectSingleNode("q1:name");

// Always check for null values. If an attribute is set to null, it is not contained in the
// resulting XML. And accessing accountidNode.text when accountidNode is null leads to
// a runtime error.
var accountid = (accountidNode == null) ? null : accountidNode.text;
var name = (nameNode == null) ? null : nameNode.text;

// finally display the values.
alert(name + ", " + accountid);
}