Saturday 15 June 2013

Trouble using xpath to get stuff out of XML with javascript -



Trouble using xpath to get stuff out of XML with javascript -

i think set namespace resolver right,

var nodes=xml.evaluate(path, //xpathexpression xml, //contextnode nsresolver, //namespaceresolver xpathresult.any_type, //resulttype null //result );

and think setting path correctly, (ive tried alot of variation here, pretty much might work)

path="/";

but nodes.iteratenext() seems telling me did wrong :

firebug output : nodes : [object xpathresult] length : undefined

the x , xml object though, because can see them , xml in firebug. if utilize code, im testing in chrome if youre using ie may have whole other can of worms. :)

heres xml (sanitized version)

<dataset xmlns="http://stub.test.data1" xmlns:xs="http://another.stub.test.moredata"> <!-- <dataset xmlns="http://stub.test.data1" xmlns:xs="http://another.stub.test.moredata" xs:schemalocation="http://yet.more.stub.test.data/xmldata.xsd" > --> <metadata> <item name="a" type="xs:string" length="92"/> <item name="b" type="xs:string" length="50"/> <item name="c" type="xs:short" precision="1"/> <item name="d" type="xs:string" length="66"/> <item name="e" type="xs:string" length="26"/> <item name="f" type="xs:string" length="6"/> <item name="g" type="xs:string" length="264"/> <item name="h" type="xs:double" precision="2"/> <item name="i" type="xs:string" length="22"/> <item name="j" type="xs:date"/> <item name="k" type="xs:date"/> <item name="l" type="xs:string" length="16"/> <item name="m" type="xs:short" precision="1"/> <item name="n" type="xs:short" precision="1"/> <item name="o" type="xs:string" length="50"/> </metadata> <data> <row> <value>somedata1</value> <value>somedata2</value> <value>somedata3</value> <value>somedata4</value> <value>somedata5</value> <value>somedata6</value> <value>somedata7</value> <value>somedata8</value> <value>somedata9</value> <value>somedata10</value> <value>somedata11</value> <value>somedata12</value> <value>somedata13</value> <value>somedata14</value> <value>somedata15</value> </row> </data> </dataset>

and heres javascript :

function loadxmldoc(dname) { if (window.xmlhttprequest) { xhttp=new xmlhttprequest(); } else { xhttp=new activexobject("microsoft.xmlhttp"); } //initializes request xhttp.open("get",dname,false);//method, url, optional async defaults true seek {xhttp.responsetype="msxml-document"} catch(err) { console.log('hey, error occured'); } // helping ie xhttp.send("");//send request. not homecoming till response returned (due false above) homecoming xhttp; } function nsresolver(nsprefix) { console.log("nsprefix : " + nsprefix); if(nsprefix == "xs") { homecoming "http://www.w3.org/2001/xmlschema-instance"; } } function displaynodes() { // code ie if (window.activexobject || xhttp.responsetype=="msxml-document") { console.log('code ie'); console.log('path=' + path); xml.setproperty("selectionlanguage","xpath"); nodes=xml.selectnodes(path); (i=0;i<nodes.length;i++) { document.write(nodes[i].childnodes[0].nodevalue); document.write("<br>"); } } // code chrome, firefox, opera, etc. else if (document.implementation && document.implementation.createdocument) { console.log('code chr / ff / op'); console.log('path=' + path); //docs : http://help.dottoro.com/ljruhkuj.php var nodes=xml.evaluate(path, //xpathexpression xml, //contextnode nsresolver, //namespaceresolver xpathresult.any_type, //resulttype null //result ); console.log("nodes : " + nodes + " length : " + nodes.length); var result=nodes.iteratenext(); while (result) { document.write(result.childnodes[0].nodevalue); document.write("<br>"); result=nodes.iteratenext(); } } document.write('shit should have displayed now'); } path="/"; function reload() { x=loadxmldoc("testxml.xml"); //x xmlhttprequest object xml=x.responsexml; //xml response request, or null if failed displaynodes(); console.log("x (xmlhttprequest object) : " + x); console.log("xml (xmlhttprequest.responsexml) : " + xml); } reload();

looking @ mozilla's documentation using xpath in javascript, seems xpathresult object has no such property length you're asking for.

so when firebug says length : undefined, doesn't mean you've done wrong in xpath path or utilize of evaluate(). thing you've done wrong can see inquire nodes.length.

if result snapshot, inquire nodes.snapshotlength, isn't:

when result type in resulttype parameter specified any_type, ... if returned result type node-set unordered_node_iterator_type.

now when iterate, should 1 result node: document root node, is, (invisible) parent of <dataset> element. next, you're asking print result.childnodes[0].nodevalue. result.childnodes[0] should <dataset> element. .nodevalue of element null, according these docs. presumably document.write() not showing anything.

instead, seek printing result.nodename (docs here). should give #document root node, or else name of element you've selected.

and if you're trying something working, , verify it's working, alter path "/*". you'll more tangible result, namely, <dataset> element.

javascript xml xpath

No comments:

Post a Comment