How To Parse XML Documents In Javascript? - AFS

AFS Integration Guide

Product
AFS
AFS_Version
7.9
Category
Reference Guide

This topic is deprecated. A better way to parse XML documents can be found using the SDK XML Package.

Most of JavaScript libraries provide easy ways to parse XML documents.

Here is an example of JavaScript parsing using jQuery (see API Reference - JQuery.parseXML for more information):

var xmlString = '<document><object><element><value>Value 1</value></element><element><value>Value 2</value></element></object><text>Hello world!</text></document>';
var $xmlDocument = $($.parseXML(xmlString));
var text = $xmlDocument.find('text').text();
console.log(text); // Prints Hello world!
var $elements = $xmlDocument.find('object element'); // Finds element tags
// find() works with CSS selectors that are documented here:
// http://api.jquery.com/category/selectors/
// For each element
$elements.each(function(index, value) {
// Retrieves the value node of the element node and extracts its text content
console.log($(value).find('value').text());
});
// Prints Value1 Value2