Skip to content Skip to sidebar Skip to footer

Parsing Xml Namespace With Jquery. Help Needed!

i am new with ajax. I have done normal xml parsing via jquery but can not get the xml with namespace working. I have searched the web and there is very few resources i found. Here

Solution 1:

I recommend using a real namespace-aware XML parser if at all possible, especially when dealing with external services. There is no guarantee that the namespace prefix will remain constant over time, for example.

Most JavaScript DOM parsers will include getElementsByTagNameNS(), which will let you find elements with the actual namespace.

The process might look something like this, assuming your data was in xml_file.

var namespace = 'http://aws.example.com/';
var parser = new DOMParser(); // Webkit, IE has its ownvar xml = parser.parseFromString(xml_file, "text/xml");    
var year = xml.getElementsByTagNameNS(namespace, 'year')[0]; // returns the first aws:year elementvar year_value = year.getAttribute('number');

Solution 2:

You can parse the DOM when you use double backslash to escape the colon

ex.

$("aws\\:year").attr('number')

edit:

the solution above does not work with webkit browsers. better solution

$("[nodeName=aws:year]").attr('number')

didn't check this out though.

Post a Comment for "Parsing Xml Namespace With Jquery. Help Needed!"