3

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

s g
  • 5,289
  • 10
  • 49
  • 82
Harshal Carpenter
  • 470
  • 1
  • 9
  • 24

2 Answers2

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).

Felipe Brahm
  • 3,162
  • 1
  • 28
  • 42
  • 1
    But how to take from a file ? – Harshal Carpenter Nov 12 '14 at 05:10
  • 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: http://api.jquery.com/jquery.ajax/ – Felipe Brahm Nov 12 '14 at 05:38
  • Yeah I am a beginner, how do get started with JQuery? .. Are JavaScript and JQuery entwined ? – Harshal Carpenter Nov 12 '14 at 05:44
  • 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. – Felipe Brahm Nov 12 '14 at 16:40
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;
}
Kartic
  • 2,935
  • 5
  • 22
  • 43
  • 1
    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. – Harshal Carpenter Nov 12 '14 at 05:42
  • 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 : http://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht – Kartic Nov 12 '14 at 05:58