Solved

How can I read this xml in javascript?

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

617 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