• 0

Reading XML documents in Firefox


Question

I am trying to read an XML file for my website. I am hitting a wall when trying to read a local (same folder as the web page) XML file and put its content on to my web page.

I have tried numerous tutorials from interweb to the same result. Firefox refuses to read the html tags for some reason. Being a total noob on this side, I would really appreciate some help on this

Here is the HTML code,

<HTML>
<HEAD>
<TITLE>Read Firefox</TITLE>
<script>
var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("someXMLfile.xmlddocument.write(xmlDoc.getElementsByTagName("employeedocument.write("<br;length = "+ xmlDoc.getElementsByTagName("employee").length);
document.write("<br;first child = "+ xmlDoc.getElementsByTagName("employee").firstChild);

&lt;/script&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;/BODY&gt;
&lt;/HTML&gt;

The XML file used,

&lt;?xml version="1.0" ?&gt;  
&lt;root&gt;
 &lt;node&gt;Node1&lt;/node&gt;
 &lt;node&gt;Node2&lt;/node&gt;
&lt;/root&gt;

I have attached both files to this post as well.

thanks in advance

try.htmlFetching info...

someXMLFile.xmlFetching info...

Link to comment
https://www.neowin.net/forum/topic/457148-reading-xml-documents-in-firefox/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Starting from the beginning:

1. You're running the script inline, which means the browser might not have the document object yet. Run the code on window.onload (or equivalent).

2. There are no <employee> nodes, so it won't find any.

3. document.getElementsByTagName() returns a collection of objects, so the most you could hope for is something like '[xmlObject collection]' when using document.write against it.

4. As there are no <employee> nodes, any call to a property of the collection will fail (it is undefined), so .length and .firstChild won't work.

5. .firstChild returns an object which when written to the document (implies .toString()) normally outputs any text nodes within it (bits within tag pairs). Is this the required behaviour or do you actually want the first child?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.