1.

Solve : XML to HTML help?

Answer»

I have been at this issue for many days now and have not found any solutions. Basically what I'm after is learning how to get BASIC XML data to display on an HTML website. It really isn't more complicated then that. The problem is, everything I've tried isn't working. It just isn't displaying the data. I'm GOING to throw in an easy example below.

I'm using IE7  but have made it so that this code should work in IE6 and IE5 as well. The object is to get the fields: TO, FROM, and MESSAGE to display the XML data that is , and . Clearly something is missing but I just cannot FIGURE it out.

----------------------------------------------------------------------------------
note.xml
----------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>REMINDER</heading>
  <body>Don't forget me this weekend!</body>
</note>
----------------------------------------------------------------------------------





----------------------------------------------------------------------------------
My HTML code
----------------------------------------------------------------------------------
<html>
<head>
<script type="text/javascript">
function loadXMLDoc(dname)
{
var xmlDoc;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET",dname,false);
  xmlDoc.send("");
  return xmlDoc.responseXML;
  }
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return xmlDoc;
  }
alert("Error loading document");
return null;
}
</script>
</head>
<body>

<h1>W3Schools Internal Note</h1>
<p>To: <span id="to"></span>

From: <span id="from"></span>

Message: <span id="message"></span>

<script type="text/javascript">
xmlDoc=loadXMLDoc("note.xml");
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>


----------------------------------------------------------------------------------

Any advice would be greatly appreciated!I don't know much XML, but, since you use W3Schools, have a look at this other example and see if you can make anything of it.

http://www.w3schools.com/xml/xml_to_html.aspBelieve me I have analyzed that website for days now and am getting nowhere. There is something I'm just not seeing.Ouch!  Seriously, I would SAVE yourself a lot of trouble and use the Prototype javascript library.  It's worth the effort to learn.  Consider using JSON to transfer data, too.Thanks, I'll check it out



Discussion

No Comment Found