1

I am trying to parse a simple xml-file using java-script. When I loaded the file into the webrowser "Chrome" the page displays nothing. enter image description here

Please let me know what is my mistake.

xml File:

< ?xml version="1.0" encoding="UTF-8" ?>
<company>
    <employee id="001" >John</employee>
    <turnover>
        <year id="2000">100,000</year>
        <year id="2001">140,000</year>
        <year id="2002">200,000</year>
    </turnover>
</company>

JAvaScript Parser:

Read XML in Microsoft Browsers</title>
<script type="text/javascript">
var xmlDoc;
function loadxml()
{
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = false;
  xmlDoc.onreadystatechange = readXML; /* to be notified when the state changes */
  xmlDoc.load("C:\Users\Amr\Desktop\files\xml.xml");
}

function readXML()
{
   if(xmlDoc.readyState == 4)
   {
    //Using documentElement Properties
    //Output company
    alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);

    //Using firstChild Properties
    //Output year
    alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);

//Using lastChild Properties
//Output average
alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);

//Using nodeValue and Attributes Properties
//Here both the statement will return you the same result
//Output 001
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);

//Using getElementByTagName Properties
//Here both the statement will return you the same result
//Output 2000
alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);

//Using text Properties
//Output John
alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);

//Using hasChildNodes Properties
//Output True
alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);
  }

1 Answer 1

2

That is because you are using an ActiveX object to do the parsing, it is only available in IE browsers. Since you are loading XML from a file, ry using XMLHttpRequest instead. It is implemented in IE since version 7 along with chrome, firefox, safari etc. Here is a good explanation and some democode for you: http://wordsfromgeek.blogspot.se/2008/11/xml-dom-in-chrome.html?m=1

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

3 Comments

If you look in the developer toolbar under console, what errormessage does it give you?
please see the attached image
That is the security message you get since you are loading a local file, it is not an error. Look at the answer in this post to allow loading of local files with ActiveX: answers.microsoft.com/en-us/ie/forum/ie9-windows_vista/…

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.