Solved

How can I read this xml in javascript?

Posted on 2012-04-09
4
313 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your help!
0
 
LVL 18

Expert Comment

by:zc2
Comment Utility
You're welcome
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

771 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

7 Experts available now in Live!

Get 1:1 Help Now