0

I'm trying to parse a simple XML file but I'm having trouble trying to obtain a value I need. The current error message I'm getting says node is not an object or is null. I'm testing on both I.E. 6, 7, FF 2,3

Here's the XML file:

<bookstore>
  <appSettings>
    <add key="myKey" value="myTargetValue"/>
  </appSettings>
</bookstore>

Here's the script I'm trying to use:

<html>
  <head>
    <title></title>
  </head>
  <script type="text/javascript">
    if (window.XMLHttpRequest)
    {
      xhttp = new window.XMLHttpRequest()
    }
    else
    {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP")
    }
    xhttp.open("GET","test.xml",false);
    xhttp.send("");
    xmlDoc=xhttp.responseXML;
    alert(xmlDoc.xml)

    var xpath = "/bookstore/appSettings/add[@key='myKey']";
    var node  = xmlDoc.selectSingleNode(xpath);
    alert(node.getAttribute("value"));    
  </script>
  <body>
  </body>
</html>

Please advise. Thank you.

1
  • your XPath returns: <add key="myKey" value="myTargetValue"/> Is this what you expect? Commented Oct 20, 2009 at 13:44

4 Answers 4

1

I would strongly advise using a library such as jQuery to make your life easier - no more document.getElementById()-ing! In jQuery you can just do:

$.ajax({
    type: 'GET',
    url: 'test.xml',
    dataType: 'xml',
    success: processXml
});

function processXml(xml)
{
    xml = $(xml);
    var value = xml.find('bookstore appSettings add[key=myKey]').attr('value');
    alert(value);
}

Edit: I'm not that familiar with XPath, so I figured I would explain the find() call in case I misread what you're trying to select from the XML. jQuery uses CSS selector syntax. So, that find() will select add nodes that have the key attribute equal to myKey, which are descendant elements of appSettings, which descend from bookstore elements.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks everyone for the help... Matt, any ideas why it's not working on IE6?
Nevermind, I think it's getting blocked by my browser security, thanks again for the help.
I still suggest you try out jQuery - it removes all the nasty browser dependency (for the most part) like having to do object detection for the XMLHttpRequest.
0

The method for evaluating your Xpath is not correct for firefox.

See https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript For a good intro to using XPath in firefox.

Also see http://www.w3.org/TR/xpath for XPath details

For your example, you would need

var node = xmlDoc.evaluate("/bookstore/appSettings/add[@key='myKey']", xmlDoc, null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;

See http://www.w3schools.com/XPath/xpath_examples.asp for How to accomplish this in both IE and firefox

2 Comments

where do you need xmlDoc.evaluate?
document.evaluate() is for DOM3 XPath supporting browsers
0

Just to make sure your XML is returning correctly, can you please alter your code:

// ...
xhttp.send("");
xmlDoc=xhttp.responseXML;

alert(xmlDoc.xml)

I suspect you don't get your XML data back from your request.

Comments

0

Before dealing with JavaScript, I would try to open the XML document directly in the browser typing the URL and see if it displays correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.