Javascript: Not reading XML correctly.

  • Thread starter Thread starter ~J~
  • Start date Start date

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
One of them days today :(

Can someone show why the following XML:

Code:
<?xml version="1.0" encoding="us-ascii" ?>
 <node text="Top" parentID="0">
  <node text="Spirits" parentID="" tag="1">
   <node text="Brandy" parentID="1" tag="B">
    <node text="Cognac / Armagnac" parentID="B" tag="10">
     <node text="Cognac" parentID="10" tag="23" />
     <node text="Armagnac" parentID="10" tag="8" />
    </node>
    <node text="Other Brandy" parentID="B" tag="19">
     <node text="Grape Brandy" parentID="19" tag="162">
      <node text="Other Grape Brandy" parentID="162" tag="125" />
     </node>
     <node text="Grape EDV" parentID="19" tag="164">
      <node text="Fine (EDV)" parentID="164" tag="92" />
      <node text="Grappa" parentID="164" tag="34" />
      <node text="Singani" parentID="164" tag="105" />
      <node text="Pisco" parentID="164" tag="102" />
      <node text="Marc / Lie" parentID="164" tag="100" />
     </node>
    </node>
   </node>
  </node>
 </node>

Doesn't render correctly please.

I'm running this code which seems to 'pop-up' all over the place for traversing XML files.

Code:
 function loadXML(xmlFile)
 {
  xmlDoc.async="false";
  xmlDoc.onreadystatechange=verify;
  xmlDoc.load(xmlFile);
 }
 function verify()
 { 
  if(xmlDoc.readyState!=4)
   return false; 
 }
function traverse(tree) {
        if(tree.hasChildNodes()) {
                document.write('<ul><li>');
                document.write('<b>'+tree.tagName+' : </b>');
                var nodes=tree.childNodes.length;
                for(var i=0; i<tree.childNodes.length; i++)
                        traverse(tree.childNodes(i));
                document.write('</li></ul>');
        }
        else
                document.write(tree.text);
}
 
 function initTraverse(file)
  {
  loadXML(file);
  var doc=xmlDoc.documentElement;
  traverse(doc);
 }

(Yes I know I'm not getting the attribute "text", I can cover that later, but it's still not rendering the correct child nodes, as if it's omitting the last one per child)

Totally stumped.
 
Yeah got that, actually have the following:

Code:
 var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 
 initTraverse("test.xml");

"for now" the IE thing isn't a problem as my testing is in IE, however would be interested to know of any cross-platform methods.
 
OK still having issues with this so going to look at it from a different angle.

Can someone recommend a good tutorial that will:

Allow me to traverse an XML document with children
Cross Platform
Ideally NOT using the Microsoft XMLDOM ActiveX component.

TIA.
 
Back
Top Bottom