Tuesday, 15 May 2012

javascript - How can I get a value from xml? -



javascript - How can I get a value from xml? -

i have next xml:

<order> <moulding> <imgsrc>imgsrc</imgsrc> <width>1.13</width> </moulding> </order>

i tried getting moulding width by:

moulding_width = doc.getelementsbytagname('moulding')[0].childnodes[1].nodevalue;

however not working. how can moulding width?

assume

var xml = "<order><moulding><imgsrc>imgsrc</imgsrc><width>1.13</width></moulding></order>";

following, xml parsing of variable string in javascript can utilize (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 method of node access

var moulding_width = parsefloat($.parsexml(xml).getelementsbytagname("order")[0].childnodes[0].childnodes[1].textcontent);

note getelementsbytagname gives list of desired elements, , not contents, why there additional childnodes compared approach.

if you're not using jquery, might consider (see http://www.w3schools.com/dom/dom_parser.asp )

function parsexml(text) { if (window.domparser) { parser=new domparser(); homecoming parser.parsefromstring(text,"text/xml"); } else { // code ie xmldoc=new activexobject("microsoft.xmldom"); xmldoc.async=false; xmldoc.loadxml(text); homecoming xmldoc; } } var moulding_width = parsefloat(parsexml(xml).getelementsbytagname("order")[0].childnodes[0].childnodes[1].textcontent);

javascript xml

No comments:

Post a Comment