Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

javascript parse xml without jquery

Hi,
I just wrote a function (with help) to parse some xml.  How can I do this without jquery?

function getValueFromXml(xml, key) {
    xmlDoc = $.parseXML(xml);
    $xml = $(xmlDoc);
    $returnValue = $xml.find(key);
    return $returnValue;
}
0
jackjohnson44
Asked:
jackjohnson44
1 Solution
 
Robert SchuttSoftware EngineerCommented:
With most new browsers this should work (tested in FireFox and Chrome):
function getValueFromXml(xml, key) {
	var parser = new DOMParser();
	var xmlDoc = parser.parseFromString(xml, "text/xml");
	var sessionIdNodes = xmlDoc.getElementsByTagName(key);
	return sessionIdNodes.length ? sessionIdNodes[0].textContent : 'NOT_FOUND';
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now