5 Replies - 4643 Views - Last Post: 21 January 2012 - 09:19 AM

#1 Tarsus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 06-September 10

XML to HTML doc with Javascript

Posted 21 January 2012 - 07:46 AM

Hi,

I'm just starting to learn javascript and trying to read in an XML document.
But something must be wrong.. I dont think the file is reading?

The xml document I have is called testXmlDoc and is located in the same folder as my html doc.

Any ideas?
Thanks.

<html>
<body>

<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","testXmlDoc.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("test text");

</script>


</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: XML to HTML doc with Javascript

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: XML to HTML doc with Javascript

Posted 21 January 2012 - 07:55 AM

And why do you think the file is not being read? Tell us exactly what's happening.
Was This Post Helpful? 0
  • +
  • -

#3 Tarsus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 06-September 10

Re: XML to HTML doc with Javascript

Posted 21 January 2012 - 08:25 AM

basically, when I run the html in my browser (IE or chrome).

I tested the JS without the XML content (see below) and it works fine. Leading me to believe that there must be some sort of issue reading the XML document, which then breaks the JS from reading document.write("test text"); when it is included

..would work. I tested

<script type="text/javascript">

document.write("test text");

</script>



The output I get from including the call to pull the XML is nothing.
Whereas, if I don't include it "test text" is displayed on the page in both browsers.

This post has been edited by Tarsus: 21 January 2012 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#4 Tarsus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 06-September 10

Re: XML to HTML doc with Javascript

Posted 21 January 2012 - 08:49 AM

I have the XML and HTML documents in the same folder.
Do I need to specify the document in any other way? ive tried folder path but that does nothing either.

[EDIT]

I have now tried to download the HTML and XML documents from the W3Schools example of a cd catalog(http://www.w3schools.com/xml/xml_to_html.asp)

I place both the HTML and XML in the same location on my computer but when I run the HTML in any browser I get nothing come up.

This post has been edited by Tarsus: 21 January 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: XML to HTML doc with Javascript

Posted 21 January 2012 - 09:09 AM

Suggestions:

1. Use Firefox and Firebug to debug the Javascript, or Chrome and its Developer Tools.
2. Upload the XML document so we can check it if you think that's the issue.
Was This Post Helpful? 0
  • +
  • -

#6 Tarsus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 06-September 10

Re: XML to HTML doc with Javascript

Posted 21 January 2012 - 09:19 AM

Sorry, just updated the post above..

Quote

I have now tried to download the HTML and XML documents from the W3Schools example of a cd catalog(http://www.w3schools.com/xml/xml_to_html.asp)

I place both the HTML and XML in the same location on my computer but when I run the HTML in any browser I get nothing come up.


So I'm thinking it must be a referencing issue.
If both files are in the same folder, can I not just refer to the file name to pull them. That is what I have been trying.


[EDIT]

They now both work in firefox but no other browser..

This post has been edited by Tarsus: 21 January 2012 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1