-1

I have the following xml:

<Order>
  <Moulding>
    <imgsrc>imgsrc</imgsrc>
    <width>1.13</width>
  </Moulding>
</Order>

I tried getting the Moulding width by:

moulding_width = doc.getElementsByTagName('Moulding')[0].childNodes[1].nodeValue;

However that is not working. How can I get the moulding width?

AllisonC
  • 2,973
  • 4
  • 29
  • 46
  • I'm not sure about the context you're working in, but this http://www.w3schools.com/xml/xml_parser.asp might help, see also http://stackoverflow.com/questions/7949752/cross-browser-javascript-xml-parsing and http://api.jquery.com/jQuery.parseXML/ – godfatherofpolka Jun 20 '14 at 15:01
  • possible duplicate of [XML parsing of a variable string in JavaScript](http://stackoverflow.com/questions/649614/xml-parsing-of-a-variable-string-in-javascript) – Patrick Hofman Jun 20 '14 at 15:02
  • What do you mean by the context I'm working in? I've looked at all those links and I'm still having trouble. – AllisonC Jun 20 '14 at 15:05
  • @AllisonC With context, I meant: Where does the XML come from or where is it located? For which browsers does it have to work? Are you using any libraries such as, e.g., jQuery? I have included a simple example answer below. – godfatherofpolka Jun 20 '14 at 15:33

2 Answers2

0

Assume

var xml = "<Order><Moulding><imgsrc>imgsrc</imgsrc><width>1.13</width></Moulding></Order>";

Following, XML parsing of a variable string in JavaScript you can use (this requires jQuery)

var order = $.parseXML(xml).getElementsByTagName("Order")[0]
var moulding = order.getElementsByTagName("Moulding")[0]
var width = moulding.getElementsByTagName("width")[0];
var moulding_width = parseFloat(width.textContent);

or, using your method of node access

var moulding_width = parseFloat($.parseXML(xml).getElementsByTagName("Order")[0].childNodes[0].childNodes[1].textContent);

Note that getElementsByTagName gives you a list of the desired elements, and not their contents, this is why there is an additional childNodes compared to your approach.

If you're not using jQuery, you might consider something like (see http://www.w3schools.com/dom/dom_parser.asp )

function parseXML(text) {
  if (window.DOMParser)  {
    parser=new DOMParser();
    return parser.parseFromString(text,"text/xml");
  } else { // code for IE
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(text);
    return xmlDoc;
  }
}

var moulding_width = parseFloat(parseXML(xml).getElementsByTagName("Order")[0].childNodes[0].childNodes[1].textContent);
Community
  • 1
  • 1
godfatherofpolka
  • 1,645
  • 1
  • 11
  • 24
0

I think this should work:

var data = "<Order><Moulding><imgsrc>imgsrc</imgsrc><width>1.13</width></Moulding></Order>";
var xml = $.parseXML(data),
$xml = $( xml ),
$test = $xml.find('width');

console.log($test.text());

I have checked this. You may also check this here: http://jsfiddle.net/5ecR6/

Check the console while you run the fiddle, you will find the log of value 1.13

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174