Select and manipulate elements in DOM in XML document in jQuery?



I would like to select elements from the DOM of an XML document in jQuery


This is what I have so far (the XML would usually come from another source not a string but this isn't relevant in this case):



var $xml = prepareXml('<measurement date="24/12/14" ammonia="0" nitrite="0" nitrate="20"></measurement><measurement date="25/12/14" ammonia="0" nitrite="1" nitrate="40"></measurement><measurement date="26/12/14" ammonia="1" nitrite="4" nitrate="55"></measurement>');

function prepareXml(string) {
var string_xml = string;
var xml_doc = $.parseXML(string_xml);
var $xml = $(xml_doc);
return $xml;
}


What I'm trying to do is something like this:



$('measurement').each( function() {
var date = $(this).attr('date');
var ammonia = $(this).attr('ammonia');
[...]
});


etc...


How can I select elements from attribute and access the DOM in the XML document rather than the HTML page the javascript is running from.


No comments:

Post a Comment