Link to home
Start Free TrialLog in
Avatar of ivanplenti
ivanplentiFlag for Afghanistan

asked on

How can I read this xml in javascript?

hi, I am new with Javascript and XML, and I would like to know how can I read this XML file

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <properties xmlns="v1">
  <comment>this is a comment</comment>
  <entry key="your.address">main.street</entry>
  <entry key="your.name">Adele</entry>
  </properties>

I need to read this xml and then show something like this using javascript

comment = this is a comment
your.address =  main.street
your.name =  Adele


I know that I can use


<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","data.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.getElementById("comment").innerHTML=
xmlDoc.getElementsByTagName("comment")[0].childNodes[0].nodeValue;
</script>

and I get the <comment> data  "this is a comment" but how can I get the data from  <entry key="your.address">main.street</entry>   for example to show something like this

comment = this is a comment
your.address =  main.street
your.name =  Adele

Thanks experts!
Avatar of zc2
zc2
Flag of United States of America image

You can do that the same way:

var entries = xmlDoc.getElementsByTagName("entry");
for( var i=0; i < entries.length; i++ ) {
    if( entries[i].getAttribute("key" ) == "your.address" )
        document.getElementById("your.address").innerHTML = entries[i].childNodes[0].nodeValue;
    if( entries[i].getAttribute("key" ) == "your.name" )
        document.getElementById("your.name").innerHTML = entries[i].childNodes[0].nodeValue;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ivanplenti

ASKER

Thank you for your help!
You're welcome