harry0812 Posted September 19, 2011 Share Posted September 19, 2011 Hi,I have a xml file in the following format<employee><name>a1</name><age>22</age> <company> <name>company1</name> <address>address1</address> </company><tenure>3yrs</tenure></employee> I want to parse the xml file in javscript without using any getelementById or getelementByName. I was able to parse the xml using nodeValue, nodeType, however I am not able to parse inside the "company" tag (as company is a second level node). I am just able to extractname=a1age=22company=tenure=3yrs. Can someone please assist me with the script to even parse inside company tag using nodeType and nodeValue.Thanks in Advance !! Link to comment Share on other sites More sharing options...
ckrudelux Posted September 19, 2011 Share Posted September 19, 2011 Could " childNodes[0] " be of any help? Link to comment Share on other sites More sharing options...
harry0812 Posted September 19, 2011 Author Share Posted September 19, 2011 Yes, i am using childnodes[0], however for some reason its not able to traverse inside the company tag. Dont know where am I making the mistake . This is the scriptfunction get_firstchild(n){x=n.firstChild;while (x.nodeType!=1) { x=x.nextSibling; }return x;}x=xmlDoc.documentElement;firstNode=get_firstchild(x);document.write(firstNode.childNodes.length);for (i=0;i<firstNode.childNodes.length;i++){if (firstNode.childNodes.nodeType==1) { //Process only element nodes document.write(firstNode.childNodes.nodeName); document.write(" = "); document.write(firstNode.childNodes.childNodes[0].nodeValue); document.write("<br />"); }} Link to comment Share on other sites More sharing options...
ckrudelux Posted September 19, 2011 Share Posted September 19, 2011 Yes, i am using childnodes[0], however for some reason its not able to traverse inside the company tag. Dont know where am I making the mistake . This is the script function get_firstchild(n){x=n.firstChild;while (x.nodeType!=1) { x=x.nextSibling; }return x;} x=xmlDoc.documentElement;firstNode=get_firstchild(x);document.write(firstNode.childNodes.length);for (i=0;i<firstNode.childNodes.length;i++){if (firstNode.childNodes.nodeType==1) { //Process only element nodes document.write(firstNode.childNodes.nodeName); document.write(" = "); document.write(firstNode.childNodes.childNodes[0].nodeValue); document.write("<br />"); }} I don't code what mush in JS but I suggest you use Firebug to see where you get the error, or you could run small blocks of the code to see where it stops also alert the values of the variables to see if they get the right value. If I would be you I would use JQuery for reading xml files, but doing it your way is better cause you will learn more from it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.