2

I also tried using loadXML() of Microsoft, but it doesn't work. It is most likely deprecated. What perceives to be wrong here. Is there any other way to write it?

The HTML code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8">
        <title>childNode Property</title>
        <script type="text/javascript" src="allfeaturetest.js"></script>
    </head>
    <body>
        <h1>childNode Property</h1>
        <hr/>
        <form name="input">
            <input type="button" value="Press me for XML" onclick="return xmlly()"/>
        </form>
        <div id="pop">
        </div>
    </body>
</html>

The JavaScript Code:

function xmlly(){
    var resul ="";
    var dom = new DOMParser();
    var xmlDoc = dom.parseFromString("address.xml","application/xml");
    var myElem = xmlDoc.getElementsByTagName("address").childNodes;
    alert(myElem); //gives me undefined
    alert(xmlDoc); //gives me [Object XMLDocument]
    document.getElementById("pop").innerHTML = xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue;
}

The XML file :

<address>
 <street>Roble Ave</street>
  <mtfcc>S1400</mtfcc>
  <streetNumber>649</streetNumber>
  <lat>37.45127</lat>
  <lng>-122.18032</lng>
  <distance>0.04</distance>
  <postalcode>94025</postalcode>
  <placename>Menlo Park</placename>
  <adminCode2>081</adminCode2>
  <adminName2>San Mateo</adminName2>
  <adminCode1>CA</adminCode1>
  <adminName1>California</adminName1>
  <countryCode>US</countryCode>
 </address>

The error shown to me:

Uncaught TypeError: Cannot read property 'nodeValue' of undefined

0

2 Answers 2

2

parseFromString will not load the data from the address.xml file. As the name says, it will only parse an XML doc from a string, like this:

var dom = new DOMParser();
var xmlDoc = dom.parseFromString("<address>test</address>","application/xml");

You'll need a separate XHR (Ajax) request to load the data from that file.

Also, you should be using console.log instead of an alert to debug this. You'll be able to actually see what's in that object (an error message in your case).

Sign up to request clarification or add additional context in comments.

4 Comments

But how to take from a file ?
That's a whole new question :) You should search on Google for "ajax" and you'll find plenty of information about this. I assume you're a beginner, so the easiest solution would be to use jQuery for this: api.jquery.com/jquery.ajax
Yeah I am a beginner, how do get started with JQuery? .. Are JavaScript and JQuery entwined ?
jQuery is the most popular JavaScript framework to help you develop web applications. It has many functions to help you manipulate the DOM, make XHR/ajax requests, etc. There are plenty of tutorials around the web.
0

If your sole purpose is to read a node, use below functions -

function xmlly()
{
var xmlDoc=loadXMLDoc("address.xml");
var elm  = xmlDoc.getElementsByTagName("street")[0]; // tag name you want to read
var node =elm.childNodes[0];
alert(node.nodeValue)
}

function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // code for IE5 and IE6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}

2 Comments

Getting this error in Chrome: XMLHttpRequest cannot load file:///C:/Users/.../ address.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.
I guess chrome is programed to watch for cross site attacks, and will block requests that is trying to read your hard drive from your web browser. You can test it in firefox. However, if you deploy the html to host, it will work fine with any browser. For more details check following link : stackoverflow.com/questions/20041656/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.