0

Here's an XML snippet:

<appSettings>
  <add key="val1" value="val2"/>

The XML document is loaded in memory, ready to be parsed.

How would you get and write the value of "val2" to the web page?

Thanks, rodchar

Post Comments:
I'm getting .selectSingleNode is not a function:

<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;

    var node = xmlDoc.selectSingleNode("/appSettings/add[@key='Key']");
    alert(node.getAttribute("value"));


</script>

3 Answers 3

1

Use jQuery, it's so much nicer.

  $(request.responseXML).find("add").each(function() {
      var marker = $(this);
      var key = marker.attr("key");
      var value = marker.attr("value");
  });
Sign up to request clarification or add additional context in comments.

2 Comments

how would i get the value "val2" (see O.P.)?
value would be val2, marker.attr accesses the value for some key. The OP is a bit confusing in this regard because the naming overlaps with this terminology. marker.attr("value") will return "val2"
1

Try this:

var node = xmlDoc.selectSingleNode("/appSettings/add[@key='val1']");
alert(node.getAttribute("value"));

Comments

1
var xmlDoc;
if (typeof DOMParser !== 'undefined') {
  xmlDoc = (new DOMParser).parseFromString(xmlText, 'text/xml');
} else {
  xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
  xmlDoc.async = 'false';
  xmlDoc.loadXML(xmlText);
}

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.