By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

DOMParser, XML and nested nodes - help needed.

P: 3
Hi, I'm trying to build a filemanager for a cms. I'd like to have a page with JS that asks the server for the filelist, which in turn answers with a XML document.

This is a sample of the XML answer:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-15"?>
  2. <items tot="1">
  3. <item iid="60dddfb31b978dc4c36d4475211e6672" key="fid" title=" C-nar.jpg">
  4. <node nType="icon" editable="false" key="thumb" width="720" height="576">[[url]]</node>
  5. <node nType="parent" editable="false">
  6.     <subnode nType="value" editable="true" key="title" title="Titolo"> C-nar.jpg</subnode>
  7.     <subnode nType="value" editable="true" key="caption" title="Descrizione">Amaro al Sapor di carciofo</subnode>
  8.     <subnode nType="value" editable="false" key="link" title="Link">[[url]]</subnode>
  9. </node>
  10.  
  11. <node nType="select" editable="true" key="uid" title="Proprietario">
  12.     <option value="1" selected="true">Amministratore</option>
  13.     <option value="10000001" selected="false">SuperAdmin</option>
  14.     <option value="2" selected="false">Pongi</option>
  15. </node>
  16. <node nType="select-multi" editable="true" key="groups:::gid" title="Visibile ai gruppi">
  17. <option value="1" selected="true">Amministratori</option>
  18. <option value="2" selected="false">Reggio Emilia</option>
  19. <option value="3" selected="true">Pippo</option>
  20. <option value="4" selected="false">Newsletter</option>
  21. </node>
  22. <node nType="value" editable="false" key="fsize">61.826KB</node>
  23. <node nType="remove" editable="false"></node>
  24. </item>
  25. </items>
I did so with a number of others scripts, but this one is the only that seems to give me problems.
Firefox doesn't alert me of XML syntax errors, but when I'm going to traverse it through Javascript I can get the first childNodes (the <item> nodes) but everything below this level is parsed as a single #text node. How could it be?
I hope you can spot a big mistake in my xml.
Thanks everyone.
Jun 21 '07 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

Can you post the code you are using?
Jun 21 '07 #2

P: 3
Ok, that's not the actual code but I just skimmed some unimportant things
Expand|Select|Wrap|Line Numbers
  1. var tb = document.getElementById("myTable").tBody;
  2. var XmlObj = parsexml(http.responseText);
  3. for(var j=0;j<XmlObj.childNodes.length;j++) {
  4.     if(XmlObj.childNodes[j].nodeType != 1) continue;
  5.     var aItem = XmlObj.childNodes[j];
  6.     aRow = document.createElement("TR");
  7.     for(var i=0;i<XmlObj.childNodes[j].childNodes[i].length;i++) {
  8.         var aItemNode = XmlObj.childNodes[j].childNodes[i];
  9.         if(aItemNode.nodeType != 1) continue;
  10.         // aValue is a Cell within the Row.
  11.         var aValue = XmlObj.childNodes[j].childNodes[i];
  12.         // append an item (aValue) to another element (aRow).
  13.         // myAppendItem(aValue, aRow);
  14.     }
  15.     tb.appendChild(aRow);
  16. }
  17.  
The inner "for" iteration runs just once to find out that the only childNode is a #text one (nodeType != 1).
Jun 21 '07 #3

acoder
Expert Mod 15k+
P: 16,027
That is probably your problem. There are 12 different node types - see link. If you want to check for a text node, check for node type 3.
Jun 22 '07 #4

P: 3
Ok, that's true, but even if I skip the check for the nodeType, it turns out to be a node with no childNodes, as if the DOMParser won't traverse completely my xml, but without warnings or errors. That's why I'm lost.
Jun 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.