Download DomDemo.java source file
Scroll down to MyAddresses.xml
Scroll down to AddrBook.dtd
// This program shows how to parse "MyAddresses.xml" with DOM, by
// first removing ignorable white space. Here we try to locate the value
// of the first NAME of the first ADDRESS. This is done in two different
// ways.
//
// Adapted 11/2005 by Wayne Pollock, Tampa Florida USA
//
// Citation:
// Listing: 8.3 From a JavaWorld article, Java Pitfalls #4, by Michael Daconta
// http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-traps_p.html
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
class DomDemo
{
public static boolean isBlank ( String buf )
{
if ( buf == null )
return false;
int len = buf.length();
for ( int i=0; i < len; ++i )
{
char c = buf.charAt( i );
if ( ! Character.isWhitespace(c) )
return false;
}
return true;
}
public static void normalizeDocument ( Node n )
{
if ( ! n.hasChildNodes() )
return;
NodeList nl = n.getChildNodes();
for ( int i = 0; i < nl.getLength(); ++i )
{
Node cn = nl.item( i );
if ( cn.getNodeType() == Node.TEXT_NODE && isBlank(cn.getNodeValue()) )
{
n.removeChild( cn );
--i;
}
else
normalizeDocument( cn );
}
}
public static Element getFirstChildElement ( Element elem )
{
if ( ! elem.hasChildNodes() )
return null;
for ( Node cn = elem.getFirstChild(); cn != null; cn = cn.getNextSibling() )
{
if ( cn.getNodeType() == Node.ELEMENT_NODE )
return (Element) cn;
}
return null;
}
public static void main ( String [] args )
{
try
{
if ( args.length != 1 )
{
System.out.println( "USAGE: java DomDemo xmlfile" );
System.exit( 1 );
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( new File(args[0]) );
// Get first Name of first Address Method 1 (look for elements):
System.out.println( "Method #1: Skip over white-space Nodes..." );
NodeList nl = doc.getElementsByTagName( "ADDRESS" );
int count = nl.getLength();
System.out.println( "\t# of \"ADDRESS\" elements: " + count );
if ( count > 0 )
{
// Get the first ADDRESS node:
Node addrNode = nl.item( 0 );
// get the NAME node of this ADDRESS node:
Node nameNode = getFirstChildElement( (Element) addrNode );
System.out.println( "\tThe first name is: "
+ nameNode.getFirstChild().getNodeValue() );
}
// Get first Name of first Address Method 2 (normalize document):
System.out.println( "\nMethod #2: Remove white-space nodes..." );
normalizeDocument( doc.getDocumentElement() );
nl = doc.getElementsByTagName( "ADDRESS" );
count = nl.getLength();
System.out.println( "\t# of \"ADDRESS\" elements: " + count );
if ( count > 0 )
{
// Get the first ADDRESS node:
Node addrNode = nl.item( 0 );
// Get the value of the NAME node of this ADDRESS node:
Node nameNode = addrNode.getFirstChild();
System.out.println( "\tThe first name is: "
+ nameNode.getFirstChild().getNodeValue());
}
}
catch ( Throwable t )
{
t.printStackTrace();
}
}
}
Download MyAddresses.xml
Scroll down to AddrBook.dtd
Scroll up to top
<?xml version="1.0"?>
<!DOCTYPE ADDRESS_BOOK SYSTEM "AddrBook.dtd">
<ADDRESS_BOOK>
<ADDRESS>
<NAME>Joe Jones </NAME>
<STREET>4332 Sunny Hill Road</STREET>
<CITY>Fairfax</CITY>
<STATE>VA</STATE>
<ZIP>21220</ZIP>
</ADDRESS>
<ADDRESS>
<NAME>Sterling Software</NAME>
<STREET> 7900 Sudley Road</STREET>
<STREET> Suite 500</STREET>
<CITY>Manassas</CITY>
<STATE>VA </STATE>
<ZIP>20109</ZIP>
</ADDRESS>
</ADDRESS_BOOK>
Download AddrBook.dtd
Scroll up to MyAddresses.xml
Scroll up to top
<!-- Address Book Markup Language Document Type Definition --> <!ELEMENT ADDRESS_BOOK (ADDRESS)+> <!ELEMENT ADDRESS (NAME, STREET+, CITY, STATE, ZIP)> <!ELEMENT NAME (#PCDATA)> <!ELEMENT STREET (#PCDATA)> <!ELEMENT CITY (#PCDATA)> <!ELEMENT STATE (#PCDATA)> <!ELEMENT ZIP (#PCDATA)> <!ATTLIST STREET TYPE (street|suiteno|aptno|other) #IMPLIED>