eHow launches Android app: Get the best of eHow on the go.

How To

How to Display XML

Member
By klickitmedia
User-Submitted Article
(0 Ratings)
XML
XML
www.KlickItMedia.com

XML (EXtensible Markup Language) is used to transport and store data into hierarchical structures. It is similar to HTML in appearance, but it does not display data. For this, we must use other programming languages. The two most common are Javascript and PHP. I will be using Javascript since it works in all browsers without having to install anything.

Difficulty: Moderate
Instructions

Things You'll Need:

  • Text Editor
  • Web browser
  • Some Javascript Experience
  1. Step 1

    Create a blank html or javacript file.

  2. Step 2
    Get the xml file
    Get the xml file

    This 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");
    }

  3. Step 3
    Write to screen
    Write to screen

    Write 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.

  4. Step 4

    view the "How To Write XML" article for the source code for books.xml

Tips & Warnings
  • Make sure to remove the white space from between all less than signs and the following character.
  • The XML file is in the How To Write XML article.
Subscribe

Post a Comment

Post a Comment

eHow Article: How to Display XML

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics