Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I read this xml in javascript?

Posted on 2012-04-09
4
Medium Priority
?
327 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 19

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 19

Accepted Solution

by:
zc2 earned 2000 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 19

Expert Comment

by:zc2
ID: 37852650
You're welcome
0

Featured Post

Industry Leaders: 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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

916 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