XML DOM Parsing in Javascript

XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript.

Example : Javascript Snippet

var str_inputxml = “<root><row id=’1′>One</row><row id=’2′>Two</row></root>”;
var obj_parser=new DOMParser;
var obj_xml = obj_parser.parseFromString(str_inputxml, “text/xml”);

var obj_rows=obj_xml.selectNodes(“/root/row”);
var text;

//Select Text Node

text = obj_rows.item(0).text;

//Select Attribute Value

var attributes = obj_xml.selectNodes(“/xml/row”);
var node;
var id;

for (var i = 0, l = attributes.length; i < l; i++) {
node = attributes.item(i);
id = node.getAttribute(‘id’);

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top