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]);
} }