sonnet.xml
这是在本教程中贯穿使用的示例 XML 文档。
<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd">
<sonnet type="Shakespearean">
<author>
<last-name>Shakespeare</last-name>
<first-name>William</first-name>
<nationality>British</nationality>
<year-of-birth>1564</year-of-birth>
<year-of-death>1616</year-of-death>
</author>
<title>Sonnet 130</title>
<text>
<line>My mistress` eyes are nothing like the sun,</line>
<line>Coral is far more red than her lips red.</line>
<line>If snow be white, why then her breasts are dun,</line>
<line>If hairs be wires, black wires grow on her head.</line>
<line>I have seen roses damasked, red and white,</line>
<line>But no such roses see I in her cheeks.</line>
<line>And in some perfumes is there more delight</line>
<line>Than in the breath that from my mistress reeks.</line>
<line>I love to hear her speak, yet well I know</line>
<line>That music hath a far more pleasing sound.</line>
<line>I grant I never saw a goddess go,</line>
<line>My mistress when she walks, treads on the ground.</line>
<line>And yet, by Heaven, I think my love as rare</line>
<line>As any she belied with false compare.</line>
</text>
</sonnet>
sonnet.dtd
这是我们示例文档所用的 DTD。
<!-- sonnet.dtd -->
<!ELEMENT sonnet (author,title?,text) >
<!ATTLIST sonnet
type (Shakespearean | Petrarchan) "Shakespearean">
<!ELEMENT text (line,line,line,line,
line,line,line,line,
line,line,line,line,
line,line) >
<!ELEMENT author (last-name,first-name,nationality,
year-of-birth?,year-of-death?) >
<!ELEMENT title (<!ELEMENT last-name (<!ELEMENT first-name (<!ELEMENT nationality (<!ELEMENT year-of-birth (<!ELEMENT year-of-death (<!ELEMENT line (
domOne.java
这是我们的第一个 DOM 应用。它解析一个 XML 文档并将其内容输出到标准输出。
/*
* (C) Copyright IBM Corp. 1999 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.*;
/**
* domOne.java
* Illustrates how to go through a DOM tree.
*/
public class domOne
{
public void parseAndPrint(String uri)
{
Document doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
// We`ve parsed the document now, so let`s print it.
if (doc != null)
printDOMTree(doc);
}
/** Prints the specified node, then prints all of its children. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println("<?xml version="1.0" ?>");
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"="" + attr.getNodeValue() +
""");
}
System.out.println(">");
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text
case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
{
System.out.print(" ");
System.out.print(data);
}
System.out.print("?>");
break;
}
}
if (type == Node.ELEMENT_NODE)
{
System.out.println();
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print(`>`);
}
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java domOne uri");
System.out.println(" where uri is the URI of the XML document you want to print.");
System.out.println(" Sample: java domOne sonnet.xml");
System.exit(1);
}
domOne d1 = new domOne();
d1.parseAndPrint(argv[0]);
}
}
domCounter.java
这段代码解析一个 XML 文档,然后遍历 DOM 树来采集有关该文档的数据。当数据采集后将其输出到标准输出。
/*
* (C) Copyright IBM Corp. 1999 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.DOMParser;
/**
* domCounter.java
* This code creates a DOM parser, parses a document, then
* prints statistics about the number and type of nodes
* found in the document.
*/
public class domCounter
{
int documentNodes = 0;
int elementNodes = 0;
int entityReferenceNodes = 0;
int cdataSections = 0;
int textNodes = 0;
int processingInstructions = 0;
public void parseAndCount(String uri)
{
Document doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
// We`ve parsed the document now, so let`s scan the DOM tree and
// print the statistics.
if (doc != null)
{
scanDOMTree(doc);
System.out.println("Document Statistics for " + uri + ":");
System.out.println("====================================");
System.out.println("Document Nodes: " + documentNodes);
System.out.println("Element Nodes: " + elementNodes);
System.out.println("Entity Reference Nodes: " + entityReferenceNodes);
System.out.println("CDATA Sections: " + cdataSections);
System.out.println("Text Nodes: " + textNodes);
System.out.println("Processing Instructions: " + processingInstructions);
System.out.println(" ----------");
int totalNodes = documentNodes + elementNodes + entityReferenceNodes +
cdataSections + textNodes + processingInstructions;
System.out.println("Total: " + totalNodes + " Nodes");
}
}
/** Scans the DOM tree and counts the different types of nodes. */
public void scanDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
case Node.DOCUMENT_NODE:
documentNodes++;
scanDOMTree(((Document)node).getDocumentElement());
break;
case Node.ELEMENT_NODE:
elementNodes++;
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
scanDOMTree(children.item(i));
}
break;
case Node.ENTITY_REFERENCE_NODE:
entityReferenceNodes++;
break;
case Node.CDATA_SECTION_NODE:
cdataSections++;
break;
case Node.TEXT_NODE:
textNodes++;
break;
case Node.PROCESSING_INSTRUCTION_NODE:
processingInstructions++;
break;
}
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java domCounter uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java domCounter sonnet.xml");
System.exit(1);
}
domCounter dc = new domCounter();
dc.parseAndCount(argv[0]);
}
}
saxOne.java
/*
* (C) Copyright IBM Corp. 1999 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.ParserFactory;
import com.ibm.xml.parsers.SAXParser;
/**
* saxOne.java
* This sample program illustrates how to use a SAX parser. It
* parses a document and writes the document? contents back to
* standard output.
*/
public class saxOne
extends HandlerBase
{
public void parseURI(String uri)
{
SAXParser parser = new SAXParser();
parser.setDocumentHandler(this);
parser.setErrorHandler(this);
try
{
parser.parse(uri);
}
catch (Exception e)
{
System.err.println(e);
}
}
/** Processing instruction. */
public void processingInstruction(String target, String data)
{
System.out.print("<?");
System.out.print(target);
if (data != null && data.length() > 0)
{
System.out.print(` `);
System.out.print(data);
}
System.out.print("?>");
}
/** Start document. */
public void startDocument()
{
System.out.println("<?xml version="1.0"?>");
}
/** Start element. */
public void startElement(String name, AttributeList attrs)
{
System.out.print("<");
System.out.print(name);
if (attrs != null)
{
int len = attrs.getLength();
for (int i = 0; i < len; i++)
{
System.out.print(" ");
System.out.print(attrs.getName(i));
System.out.print("="");
System.out.print(attrs.getValue(i));
System.out.print(""");
}
}
System.out.print(">");
}
/** Characters. */
public void characters(char ch[], int start, int length)
{
System.out.print(new String(ch, start, length));
}
/** Ignorable whitespace. */
public void ignorableWhitespace(char ch[], int start, int length)
{
characters(ch, start, length);
}
/** End element. */
public void endElement(String name)
{
System.out.print("</");
System.out.print(name);
System.out.print(">");
}
/** End document. */
public void endDocument()
{
// No need to do anything.
}
//
// ErrorHandler methods
//
/** Warning. */
public void warning(SAXParseException ex)
{
System.err.println("[Warning] "+
getLocationString(ex)+": "+
ex.getMessage());
}
/** Error. */
public void error(SAXParseException ex)
{
System.err.println("[Error] "+
getLocationString(ex)+": "+
ex.getMessage());
}
/** Fatal error. */
public void fatalError(SAXParseException ex)
throws SAXException
{
System.err.println("[Fatal Error] "+
getLocationString(ex)+": "+
ex.getMessage());
throw ex;
}
/** Returns a string of the location. */
private String getLocationString(SAXParseException ex)
{
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null)
{
int index = systemId.lastIndexOf(`/`);
if (index != -1)
systemId = systemId.substring(index + 1);
str.append(systemId);
}
str.append(`:`);
str.append(ex.getLineNumber());
str.append(`:`);
str.append(ex.getColumnNumber());
return str.toString();
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java saxOne uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java saxOne sonnet.xml");
System.exit(1);
}
saxOne s1 = new saxOne();
s1.parseURI(argv[0]);
}
}
saxCounter.java
/*
* (C) Copyright IBM Corp. 1999 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.ParserFactory;
import com.ibm.xml.parsers.SAXParser;
/**
* saxCounter.java
* This sample program calculates statistics for an XML document,
* based on the SAX events received. When the parse is complete,
* it prints the statistics to standard output.
*/
public class saxCounter
extends HandlerBase
{
int startDocumentEvents = 0;
int endDocumentEvents = 0;
int startElementEvents = 0;
int endElementEvents = 0;
int processingInstructionEvents = 0;
int characterEvents = 0;
int ignorableWhitespaceEvents = 0;
int warningEvents = 0;
int errorEvents = 0;
int fatalErrorEvents = 0;
public void parseURI(String uri)
{
SAXParser parser = new SAXParser();
parser.setDocumentHandler(this);
parser.setErrorHandler(this);
try
{
parser.parse(uri);
}
catch (Exception e)
{
System.err.println(e);
}
System.out.println("Document Statistics for " + uri + ":");
System.out.println("====================================");
System.out.println("DocumentHandler Events:");
System.out.println(" startDocument " +
startDocumentEvents);
System.out.println(" endDocument " +
endDocumentEvents);
System.out.println(" startElement " +
startElementEvents);
System.out.println(" endElement " +
endElementEvents);
System.out.println(" processingInstruction " +
processingInstructionEvents);
System.out.println(" character " +
characterEvents);
System.out.println(" ignorableWhitespace " +
ignorableWhitespaceEvents);
System.out.println("ErrorHandler Events:");
System.out.println(" warning " +
warningEvents);
System.out.println(" error " +
errorEvents);
System.out.println(" fatalError " +
fatalErrorEvents);
System.out.println(" ----------");
int totalEvents = startDocumentEvents + endDocumentEvents +
startElementEvents + endElementEvents +
processingInstructionEvents +
characterEvents + ignorableWhitespaceEvents +
warningEvents + errorEvents + fatalErrorEvents;
System.out.println("Total: " +
totalEvents + " Events");
}
/** Processing instruction. */
public void processingInstruction(String target, String data)
{
processingInstructionEvents++;
}
/** Start document. */
public void startDocument()
{
startDocumentEvents++;
}
/** Start element. */
public void startElement(String name, AttributeList attrs)
{
startElementEvents++;
}
/** Characters. */
public void characters(char ch[], int start, int length)
{
characterEvents++;
}
/** Ignorable whitespace. */
public void ignorableWhitespace(char ch[], int start, int length)
{
ignorableWhitespaceEvents++;
}
/** End element. */
public void endElement(String name)
{
endElementEvents++;
}
/** End document. */
public void endDocument()
{
endDocumentEvents++;
}
//
// ErrorHandler methods
//
/** Warning. */
public void warning(SAXParseException ex)
{
warningEvents++;
}
/** Error. */
public void error(SAXParseException ex)
{
errorEvents++;
}
/** Fatal error. */
public void fatalError(SAXParseException ex)
throws SAXException
{
fatalErrorEvents++;
throw ex;
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java saxCounter uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java saxCounter sonnet.xml");
System.exit(1);
}
saxCounter sc = new saxCounter();
sc.parseURI(argv[0]);
} }