Solved

How can I read this xml in javascript?

Posted on 2012-04-09
4
322 Views
Last Modified: 2012-04-16
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!
0
Comment
Question by:ivanplenti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 37825616
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

0
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 37825665
On the second thought, it's more convenient way is to use XPath, like in the sample below:

function nsResolver(prefix) {
    return prefix == "v1" ? "v1" : "xmlns:v1='v1'";

}

function selectNodes( xml, query ){
    if( typeof xml.selectNodes != 'undefined' ){ // code for IE
        xml.setProperty( "SelectionNamespaces", nsResolver() );
        return xml.selectNodes(query);
    } else if( document.implementation.hasFeature('XPath', '3.0') ) {
        try { // code for IE7+, Firefox, Chrome, Opera, Safari
            var nodes = [];
            var items = xml.evaluate(query, xml, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            for(var i=0; i < items.snapshotLength; i++){
                nodes[i] = items.snapshotItem(i);
            }
            return nodes;
        } catch( e ) {
            console.error( e.message );
        }
    } else
        return [];
}

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=selectNodes( xmlDoc, "//v1:comment/text()" )[0].nodeValue;
document.getElementById("your.address").innerHTML=selectNodes( xmlDoc, "//v1:entry[@key='your.address']/text()" )[0].nodeValue;
document.getElementById("your.name").innerHTML=selectNodes( xmlDoc, "//v1:entry[@key='your.name']/text()" )[0].nodeValue;

Open in new window

0
 

Author Closing Comment

by:ivanplenti
ID: 37852648
Thank you for your help!
0
 
LVL 18

Expert Comment

by:zc2
ID: 37852650
You're welcome
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question