Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

Working with XML Data Yourself

Say that you want to extract the data from an XML document yourself, and to work with that data, rather than simply telling a browser how to display it. For example, suppose you want to extract the text from our <heading> element:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="ch01_04.css"?>
<document>
    <heading>

           Hello From XML

       </heading>
    <message>
        This is an XML document!
    </message>
</document>

One way of gaining access to that data in a browser is to use JavaScript, which browsers like Internet Explorer and Netscape Navigator support. We'll work through that process step by step in Day 15, "Using JavaScript and XML," but you can see a sample HTML page with embedded JavaScript that will do the trick in Listing 1.5.

Example 1.5. Extracting Data from an XML Document Using JavaScript (ch01_05.html)

<HTML>
    <HEAD>
         <TITLE>
             Retrieving data from an XML document
         </TITLE>

         <XML ID="firstXML" SRC="ch01_02.xml"></XML>

         <SCRIPT LANGUAGE="JavaScript">
             function getData()
             {
                  xmldoc= document.all("firstXML").XMLDocument;

                  nodeDoc = xmldoc.documentElement;
                  nodeHeading = nodeDoc.firstChild;

                  outputMessage = "Heading: " +
                        nodeHeading.firstChild.nodeValue;
                  message.innerHTML=outputMessage;
             }
         </SCRIPT>
    </HEAD>

    <BODY>
        <CENTER>
            <H1>
                 Retrieving data from an XML document
            </H1>

            <DIV ID="message"></DIV>
            <P>
            <INPUT TYPE="BUTTON" VALUE="Read the heading"
                ONCLICK="getData()">
        </CENTER>
    </BODY>
</HTML>

When you open this example in Internet Explorer, it displays a button with the caption "Read the heading", as you see in Figure 1.5. When you click that button, the JavaScript reads the text in the <heading> element in our sample XML document, ch01_02.xml, and displays that text, as you see in the figure. In this way, we've been able to extract data from an XML document—and when you've extracted your data from an XML document, you're free to work on it as you like.

01fig05.gif

Figure 1.5 Extracting data from an XML document in Internet Explorer.

We'll also take a look at using the Java programming language to handle XML in Day 16, "Using Java and .NET: DOM," and Day 17, "Using Java and .NET: SAX." Java has all kinds of built-in support for working with XML, and you can see a sample Java program in Listing 1.6. Like our JavaScript example, this example reads the text content of the <heading> element in our sample XML document, ch01_02.xml, and displays that text.

Example 1.6. Extracting Data from an XML Document Using Java (ch01_06.java)

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class ch01_06
{
    static public void main(String[] argv)
    {
        try {

        DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();

        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
        }
        catch (ParserConfigurationException pce) {}

        Document doc = null;
            doc = db.parse("ch01_02.xml");

            for (Node node = doc.getDocumentElement().getFirstChild();
                node != null; node = node.getNextSibling()) {

                if (node instanceof Element) {
                    if (node.getNodeName().equals("heading")) {

                        StringBuffer buffer = new StringBuffer();

                        for (Node subnode = node.getFirstChild();
                            subnode != null; subnode = subnode. getNextSibling()){
                            if (subnode instanceof Text) {
                                buffer.append(subnode.getNodeValue());
                            }
                        }
                        System.out.println(buffer.toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When you run this program (see Day 16 for the details), the output looks like this:

%java ch01_06
        Hello From XML

As you can see, it's possible to extract data from an XML document, so someone else can write such a document using tags you both agree on, send you that document over the Internet, and you can extract the data you need from the document by searching for elements with specific names. There are thousands of Web-based applications these days, and they've sent and interpreted thousands of XML documents in the time it took you to read this sentence.

Share ThisShare This

Informit Network