Sams Teach Yourself XML in 21 Days
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- Day 2. Creating XML Documents
- Day 3. Creating Well-Formed XML Documents
- Day 4. Creating Valid XML Documents: DTDs
- Declaring Attributes in DTDs
- Day 6. Creating Valid XML Documents: XML Schemas
- Day 7. Creating Types in XML Schemas
- Part I. In Review
- Day 8. Formatting XML by Using Cascading Style Sheets
- Day 9. Formatting XML by Using XSLT
- Day 10. Working with XSL Formatting Objects
- Part II. In Review
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Day 12. Putting XHTML to Work
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Day 14. Handling XLinks, XPointers, and XForms
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Day 16. Using Java and .NET: DOM
- Day 17. Using Java and .NET: SAX
- Day 18. Working with SOAP and RDF
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Day 20. Working with XML and Databases
- Day 21. Handling XML in .NET
- Part V. In Review
- Appendix A. Quiz Answers
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.
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.
Structuring Your Data | Next Section

Account Sign In
View your cart