Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This

Parsing XML Yourself

Say that you have this XML document, ch01_02.xml, which we developed earlier in this chapter:

<?xml version="1.0" encoding="UTF-8"?> 
<DOCUMENT>
  <GREETING>
    Hello From XML
  </GREETING>
  <MESSAGE>
    Welcome to the wild and woolly world of XML.
  </MESSAGE>
</DOCUMENT>

Now say that you wanted to extract the greeting Hello From XML from this XML document. One way of doing that is by using XML data islands in Internet Explorer and then using a scripting language, such as JavaScript, to extract and display the text content of the <GREETING> element. Here's how that looks in a Web page:

Listing ch01_08.html

<HTML>
  <HEAD>
     <TITLE>
       Finding Element Values in 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;
         nodeGreeting = nodeDoc.firstChild;

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

  <BODY>
    <CENTER>
      <H1>
        Finding Element Values in an XML Document
      </H1>

      <DIV ID="message"></DIV>
      <P>
      <INPUT TYPE="BUTTON" VALUE="Get The Greeting" 
        ONCLICK="getData()">
    </CENTER>
  </BODY>
</HTML>

This Web page displays a button with the caption Get The Greeting, as you see in Figure 1-5. When you click the button, the JavaScript code in this page reads in ch01_02.xml, extracts the text from the <GREETING> element, and displays that text, as you can also see in Figure 1-5. In this way, you can see how you can create applications that handle XML documents in a customized way—even customized XML browsers.

Figure 1-5Figure 1-5 Extracting data from an XML document in Internet Explorer.

We'll see more about using JavaScript to work with XML later in this book. I'll also cover how JavaScript itself works first, so that if you haven't used it before, there won't be a problem.

Although JavaScript is useful for lightweight XML uses, most XML programming is done in Java today, and we'll take advantage of that in this book as well. Here's an example Java program, ch01_09.java. This program also reads ch01_02.xml and extracts the text content of the <GREETING> element (we'll see how to run this kind of program in Chapter 10, "Understanding Java"):

Listing ch01_09.java

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

public class ch01_09 
{
  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("GREETING")) {
            
            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, the output looks like this (I'll use % to stand for the command-line prompt in this book. If you're using UNIX, this prompt might look familiar, or your prompt might look something like \home\steve:. If you're using Windows, you get a command-line prompt by opening an MS-DOS window, and your prompt might look something like C:\XML>):

%java ch01_09
    Hello From XML

(Note that this program returns all the text in the <GREETING> element, including the leading spaces.) We'll see how to use Java to parse XML documents later in this book, in Chapter 11, "Java and the XML DOM," and Chapter 12, "Java and SAX." I'll also cover the Java you need to know before getting into the programming; if you haven't programmed in Java before, that won't be a problem.

We've gotten a good overview of how XML works. Now it's time to take a look at how it's already working in the real world, starting with an overview of the XML resources available to you.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.