Things You'll Need:
- Text Editor
- Web browser
- Some Javascript Experience
-
Step 1
Create a blank html or javacript file.
-
Step 2
Get the xml fileThis step gets the xml file "books.xml" which can be found in the related eHow articles. There are two different ways of getting the file (Microsoft always has to do their own thing). This code makes sure it works across multiple browsers. Insert this into the html or javascript file. It grabs the file books.xml and loads it to the variable myXML.
var myXML;
if (window.XMLHttpRequest)
{
myXML=new window.XMLHttpRequest();
myXML.open("GET","books.xml",false);
myXML.send("");
myXML=myXML.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
myXML=new ActiveXObject("Microsoft.XMLDOM");
myXML.async=false;
myXML.load("books.xml");
} -
Step 3
Write to screenWrite the variable to the screen. You must remove the white space between the less than signs and the following character. eHow will not let this article print without the space. The coding is wrong with the space.
document.write("< div>");
var x=myXML.getElementsByTagName("book");
for (i=0;i< x.length;i++)
{
document.write("< span>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("< /span>< br />< span>");
document.write(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue);
document.write(": < /span>< br />< span>");
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
document.write("< /span>");
}
document.write("< /div>");
This uses a built in Javascript function called getElementsByTagName() to grab each XML node. -
Step 4
view the "How To Write XML" article for the source code for books.xml










