Solved

How can I read this xml in javascript?

Posted on 2012-04-09
4
316 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now