Jump to content

XML Viewing


Imoddedu

Recommended Posts

The problem is that only "Latest News" shows, the rest doesn't, and I have no idea what to do >.< Here is the code

<script type="text/javascript">if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // for older IE 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","news.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;document.write("<h3>Latest News</h3>");document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;</script>

Link to comment
Share on other sites

WHERE is the script? The code is in the global space, so it runs as soon as that section of the page loads. If this script is in your <head> element, it will fail, because "author" and all those other elements don't exist yet.For this to work as it is written, the script must be physically placed BELOW "author" and those other elements.Otherwise, the code should all be placed in a function to be called on page load.If that is not the problem, then you might try alerting the value of your XML nodes just to see if they have values.Note that if you try to access the value of a text node and the node is empty, you will throw an error and the script will terminate. It is good practice to test the length of any childNodes collection before trying to access its members. The simplest thing is to write your own getNodeValue function to automate this, and have it return an empty string if no child elements exist.

Link to comment
Share on other sites

FWIW, I pulled this out of an old project. I think it still works (the project is still running) but it's been a while since I really looked at it:

function get_node_value (n) {   if (!n) return "err";   return n.childNodes[0] ? n.childNodes[0].nodeValue : "";}// use it like this:function populate ()   var myNode = xmlDoc.getElementsByTagName("author")[0];   var val = get_node_value (myNode);   document.getElementById("author").innerHTML = val;   // or combine all that and use it more tersely:   document.getElementById("author").innerHTML = get_node_value (xmlDoc.getElementsByTagName("author")[0]);}

Link to comment
Share on other sites

Ah Ok, I see the problem now thanks for the help!EDIT***I got it to display 1 item, how can I display all three items of the XML file?

Link to comment
Share on other sites

Ok, here is the javascript code:

<!-- Start of XML Feed (News) -->	<script type="text/javascript">if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // for older IE 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","news.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;</script><!-- End XML Feed (News) -->

and the XML

<?xml version="1.0"?><news>	<author>Name</author>	<date>Wednesday, May 25th, 2010</date>	<title>Reminder</title>	<description>Don't forget to edit this!</description>		<author>Name_01</author>	<date>Wednesday, May 27th, 2010</date>	<title>Title goes here</title>	<description>Should Edit this!</description>	</news>

Link to comment
Share on other sites

Each element only has one childNode. Using a childNodes index greater than 0 will throw an error. To get a different element, you need to change the index of the collection returned by getElementsByTagName:xmlDoc.getElementsByTagName("author")[change me]

Link to comment
Share on other sites

Now what happens is that it shows the last XML thing.XML Code:

<?xml version="1.0"?><news>	<author>name</author>	<date>Wednesday, May 25th, 2010</date>	<title>Reminder</title>	<description>Don't forget to edit this!</description>		<author>name</author>	<date>Wednesday, May 26th, 2010</date>	<title>You...</title>	<description>Should Edit this!</description>		<author>name</author>	<date>Wednesday, May 27th, 2010</date>	<title>Me!</title>	<description>I'm on your wall!</description> ----That is what it shows, that entry	</news>

and the javascript

<script type="text/javascript">if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // for older IE 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","news.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[1].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[1].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[1].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[1].childNodes[0].nodeValue;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[2].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[2].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[2].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[2].childNodes[0].nodeValue;</script>

Link to comment
Share on other sites

Yup. It actually does display the contents of all three, but each time you change the index you are erasing the contents of the HTML elements and replacing it with the new contents.What would you like the HTML to look like when all the content has been added?

Link to comment
Share on other sites

If you mean something like this:authordatetitledescriptionauthordatetitledescriptionauthordatetitledescriptionthen you will need 3 sets of HTML elements, and each element will need a unique id, like author0, author1, author2, and so on.

Link to comment
Share on other sites

Now the page just has:By: ,By: ,By: , Here it is:HTML w/ JavaScript

<ul>	<li>By: <span id="author0"></span>, <span id="date0"></span><br /><b><span id="title0"></b></span><br /><span id="description0"></span></li>	<li>By: <span id="author1"></span>, <span id="date1"></span><br /><b><span id="title1"></b></span><br /><span id="description1"></span></li>	<li>By: <span id="author2"></span>, <span id="date2"></span><br /><b><span id="title2"></b></span><br /><span id="description2"></span></li>	</ul><!-- Start of XML Feed (News) -->	<script type="text/javascript">if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // for older IE 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","news.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[1].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[1].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[1].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[1].childNodes[0].nodeValue;document.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[2].childNodes[0].nodeValue;document.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[2].childNodes[0].nodeValue;document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[2].childNodes[0].nodeValue;document.getElementById("description").innerHTML=xmlDoc.getElementsByTagName("description")[2].childNodes[0].nodeValue;</script><!-- End XML Feed (News) -->

and the XML

<?xml version="1.0"?><news>	<author>name</author>	<date>Wednesday, May 25th, 2010</date>	<title>Reminder</title>	<description>Don't forget to edit this!</description>		<author>name</author>	<date>Wednesday, May 26th, 2010</date>	<title>You...</title>	<description>Should Edit this!</description>		<author>name</author>	<date>Wednesday, May 27th, 2010</date>	<title>Me!</title>	<description>I'm on your wall!</description>	</news>

Link to comment
Share on other sites

I assumed you would understand that you would have to change these references to correspond:document.getElementById("author")becomesdocument.getElementById("author0")and so on. I am sorry if that was not clear.

Link to comment
Share on other sites

Nah it's fine, it takes me a while to realize some things! haha :) Again, the same problem.

	<script type="text/javascript">if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else // for older IE 5/6  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET","news.xml",false);xhttp.send("");xmlDoc=xhttp.responseXML;document.getElementById("author0").innerHTML=xmlDoc.getElementsByTagName("author0")[0].childNodes[0].nodeValue;document.getElementById("date0").innerHTML=xmlDoc.getElementsByTagName("date0")[0].childNodes[0].nodeValue;document.getElementById("title0").innerHTML=xmlDoc.getElementsByTagName("title0")[0].childNodes[0].nodeValue;document.getElementById("description0").innerHTML=xmlDoc.getElementsByTagName("description0")[0].childNodes[0].nodeValue;document.getElementById("author1").innerHTML=xmlDoc.getElementsByTagName("author1")[0].childNodes[0].nodeValue;document.getElementById("date1").innerHTML=xmlDoc.getElementsByTagName("date1")[0].childNodes[0].nodeValue;document.getElementById("title1").innerHTML=xmlDoc.getElementsByTagName("title1")[0].childNodes[0].nodeValue;document.getElementById("description1").innerHTML=xmlDoc.getElementsByTagName("description1")[0].childNodes[0].nodeValue;document.getElementById("author2").innerHTML=xmlDoc.getElementsByTagName("author2")[0].childNodes[0].nodeValue;document.getElementById("date2").innerHTML=xmlDoc.getElementsByTagName("date2")[0].childNodes[0].nodeValue;document.getElementById("title2").innerHTML=xmlDoc.getElementsByTagName("title2")[0].childNodes[0].nodeValue;document.getElementById("description2").innerHTML=xmlDoc.getElementsByTagName("description2")[0].childNodes[0].nodeValue;</script><!-- End XML Feed (News) -->

and again, the XML :)

<?xml version="1.0"?><news>	<author0>name</author0>	<date0>Wednesday, May 25th, 2010</date0>	<title>Reminder</title>	<description>Don't forget to edit this!</description>		<author1>name</author1>	<date1>Wednesday, May 26th, 2010</date1>	<title1>You...</title1>	<description1>Should Edit this!</description1>		<author2>name</author2>	<date2>Wednesday, May 27th, 2010</date2>	<title2>Me!</title2>	<description2>I'm on your wall!</description2>	</news>

Link to comment
Share on other sites

I must have missed your reply. Sorry about that. :) The immediate problem is that when you changed your XML, you did not do it completely. So you have no element called <title0>, and that is where your script terminates.The bigger problem is that you changed your XML!!! There was no need for that. I have revised your code (it did not take long) to show you what will work. Please notice how I change the index values that we discussed in Post #8.XML

<author>name</author><date>Wednesday, May 25th, 2010</date><title>Reminder</title><description>Don't forget to edit this!</description><author>name</author><date>Wednesday, May 26th, 2010</date><title>You...</title><description>Should Edit this!</description><author>name</author><date>Wednesday, May 27th, 2010</date><title>Me!</title><description>I'm on your wall!</description>

JavaScript

document.getElementById("author0").innerHTML=xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue;document.getElementById("date0").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;document.getElementById("title0").innerHTML=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;document.getElementById("description0").innerHTML=xmlDoc.getElementsByTagName("description")[0].childNodes[0].nodeValue;document.getElementById("author1").innerHTML=xmlDoc.getElementsByTagName("author")[1].childNodes[0].nodeValue;document.getElementById("date1").innerHTML=xmlDoc.getElementsByTagName("date")[1].childNodes[0].nodeValue;document.getElementById("title1").innerHTML=xmlDoc.getElementsByTagName("title")[1].childNodes[0].nodeValue;document.getElementById("description1").innerHTML=xmlDoc.getElementsByTagName("description")[1].childNodes[0].nodeValue;document.getElementById("author2").innerHTML=xmlDoc.getElementsByTagName("author")[2].childNodes[0].nodeValue;document.getElementById("date2").innerHTML=xmlDoc.getElementsByTagName("date")[2].childNodes[0].nodeValue;document.getElementById("title2").innerHTML=xmlDoc.getElementsByTagName("title")[2].childNodes[0].nodeValue;document.getElementById("description2").innerHTML=xmlDoc.getElementsByTagName("description")[2].childNodes[0].nodeValue;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...