nodeValue is 'null' - why?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pascalwitzig
    New Member
    • Aug 2007
    • 3

    #1

    nodeValue is 'null' - why?!

    Hi Guys,

    I'm a newbie to XML so I tried to create my first "hello-world". Actually I already failed here. The Code returns 'null'. I'm aware of the white-space bug and I actually think, that I avoided it. So have a look:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
    <head>
    	<title>JavaScript und XML</title>
    <script type="text/javascript">
    //<![CDATA[
    var loadXML = function(xmlDoc)
    {
        if (window.ActiveXObject)
        {
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.load(xmlDoc);
            parseXML();
        }
        else if (document.implementation && document.implementation.createDocument)
        {
            xml = document.implementation.createDocument("","",null);
            xml.async = false;
            xml.onload = parseXML;
            xml.load(xmlDoc);
        }
        else
        {
            alert("Can't open XML-Page.");
        }
    }
    window.onload = function()
    {
        loadXML("blumen.xml");
    }
    
    var parseXML = function()
    {
        var x=xml.documentElement.getElementsByTagName('name')[0].nodeValue;
    	alert(x);
    
    }
    
    //]]>
    </script>
    
    </head>
    <body>
    
    
    </body>
    </html>
    and here's the XML-file "blumen.xml ":

    Code:
    <?xml version="1.0" ?>\
    <root>\
        <blume>\
            <name>Rose</name>\
            <preis>1.10</preis>\
        </blume>\
        <blume>\
            <name>Tulpe</name>\
            <preis>0.90</preis>\
        </blume>\
    </root>
    What am I doing wrong?!

    Thanks for ur help!
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    Hi,

    Does the script have access to open the file? If the page is running as a local page, ie not through http protocol then is would not be able to access the file, if this is through http then it should be able to access the file and I will keep looking at it.

    Also, just noticed that there seems to be a trailing slash "\" at the end of each line, including the declaration, is this the case or is this a result of copying the file as this could also cause an issue.

    Comment

    • markrawlingson
      Recognized Expert Contributor
      • Aug 2007
      • 346

      #3
      The problem is in the function parseXML.

      [CODE=javascript]
      var parseXML = function()
      {
      var x=xml.documentE lement.getEleme ntsByTagName('n ame') [0].nodeValue;
      alert(x);

      }
      [/CODE]

      I ran your code, but recoded your parseXML function, and it returned the name "rose" - as it should.

      [CODE=javascript]
      var parseXML = function()
      {
      var x = xml.getElements ByTagName('name ')[0].text;
      alert(x);
      }
      [/CODE]

      Comment

      Working...