XML/JSP婵炴垶鏌ㄩ鍡楊潖閿燂拷
Hello,XML XML&Java/JSP濠殿喗甯掗崐浠嬪醇閿燂拷 JSP+XML闂佺懓鐏堥崑鎾绘煛閸偄鐏g紓鍫熷閹蹭即鏁撻敓锟� JSP+XML闂佸搫顑呯€氼噣鎮甸婧惧亾閸︻厼浠掔紒璁规嫹-Sparks.com Serving XML with JSP XML缂傚倷绀佺换鎰板触閳ь剟姊婚崒銈呮灓闁烩槄鎷� IBM闂佹眹鍔岄崹鏄匧/Java闂佽桨鐒﹂悷褔鍩㈤敓锟� **闂佽桨鐒﹂悷褔鍩㈤敓锟� 缂備焦顨愰幏锟�1-3缂備焦妫戦幏锟� **闂佽桨鐒﹂悷褔鍩㈤敓锟� 缂備焦顨愰幏锟�4缂備焦妫戦幏锟� **闂佽桨鐒﹂悷褔鍩㈤敓锟� 缂備焦顨愰幏锟�5缂備焦妫戦幏锟� **闂佽桨鐒﹂悷褔鍩㈤敓锟� 闂傚倸瀚€氼剛绱為敓锟�1 濠电姍鍕闁宠鍚嬮幆鏃堟晸閿燂拷 **闂佽桨鐒﹂悷褔鍩㈤敓锟� 闂傚倸瀚€氼剛绱為敓锟�2 濠电姍鍕闁宠鍚嬮幆鏃堟晸閿燂拷 闁荤姳闄嶉崐娑㈡儊閹便帩L Server Java婵炴垶鎸搁柅妗礚闂佹眹鍔岀€氼剟鏌﹂埡鍐╁仒閻忕偠鍋愰悷顖炴煟閿濆繑瀚� JDOM--XML闂佹眹鍔岄崵妾僾a闂佺顕х换妤呭醇閿燂拷 Apache闂佹眹鍔岄崹鏄匧婵$偑鍊曞﹢鍗灻洪敓锟� Cocoon闂佹眹鍔岀€氼剟鎮鹃妸褎鍟戦柨鐕傛嫹 XML DTD婵炲濮撮鍥╁垝閿燂拷 XML Schema闂佺ǹ绻堥崕闈浳涢敓锟� JSP婵炴垶鎸搁柅妗礚闂佹眹鍔岀€氼垱绂掗崼銉ョ闁跨噦鎷� XML婵炴垶鎸告稉娓乿a闂佹眹鍔岀€氼參寮抽悢鐓庣妞ゆ柨澧介幏銊╂倵閻у憡瀚�
|
Java XML教程
附录2
来源:http://d23xapp2.cn.ibm.com/developerWorks/education/xml/xmljava/tutorial/xmljava-1-1.html
domBuilder.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.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.*;
/**
* domBuilder.java
* This sample program illustrates how to create a DOM tree from scratch.
*/
public class domBuilder
{
/** Prints the specified node, recursively. */
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 == 1 && argv[0].equals("-help"))
{
System.out.println("Usage: java domBuilder");
System.out.println(" This code builds a DOM tree, then prints it.");
System.exit(1);
}
try
{
Document doc = (Document)Class.
forName("com.ibm.xml.dom.DocumentImpl").
newInstance();
Element root = doc.createElement("sonnet");
root.setAttribute("type", "Shakespearean");
Element author = doc.createElement("author");
Element lastName = doc.createElement("last-name");
lastName.appendChild(doc.createTextNode("Shakespeare"));
author.appendChild(lastName);
Element firstName = doc.createElement("first-name");
firstName.appendChild(doc.createTextNode("William"));
author.appendChild(firstName);
Element nationality = doc.createElement("nationality");
nationality.appendChild(doc.createTextNode("British"));
author.appendChild(nationality);
Element yearOfBirth = doc.createElement("year-of-birth");
yearOfBirth.appendChild(doc.createTextNode("1564"));
author.appendChild(yearOfBirth);
Element yearOfDeath = doc.createElement("year-of-death");
yearOfDeath.appendChild(doc.createTextNode("1616"));
author.appendChild(yearOfDeath);
root.appendChild(author);
Element title = doc.createElement("title");
title.appendChild(doc.createTextNode("Sonnet 130"));
root.appendChild(title);
Element lines = doc.createElement("lines");
Element line01 = doc.createElement("line");
line01.appendChild(doc.createTextNode("My mistress' eyes are nothing like the sun,"));
lines.appendChild(line01);
Element line02 = doc.createElement("line");
line02.appendChild(doc.createTextNode("Coral is far more red than her lips red."));
lines.appendChild(line02);
Element line03 = doc.createElement("line");
line03.appendChild(doc.createTextNode("If snow be white, why then her breasts are dun,"));
lines.appendChild(line03);
Element line04 = doc.createElement("line");
line04.appendChild(doc.createTextNode("If hairs be wires, black wires grow on her head."));
lines.appendChild(line04);
Element line05 = doc.createElement("line");
line05.appendChild(doc.createTextNode("I have seen roses damasked, red and white,"));
lines.appendChild(line05);
Element line06 = doc.createElement("line");
line06.appendChild(doc.createTextNode("But no such roses see I in her cheeks."));
lines.appendChild(line06);
Element line07 = doc.createElement("line");
line07.appendChild(doc.createTextNode("And in some perfumes is there more delight"));
lines.appendChild(line07);
Element line08 = doc.createElement("line");
line08.appendChild(doc.createTextNode("Than in the breath that from my mistress reeks."));
lines.appendChild(line08);
Element line09 = doc.createElement("line");
line09.appendChild(doc.createTextNode("I love to hear her speak, yet well I know"));
lines.appendChild(line09);
Element line10 = doc.createElement("line");
line10.appendChild(doc.createTextNode("That music hath a far more pleasing sound."));
lines.appendChild(line10);
Element line11 = doc.createElement("line");
line11.appendChild(doc.createTextNode("I grant I never saw a goddess go,"));
lines.appendChild(line11);
Element line12 = doc.createElement("line");
line12.appendChild(doc.createTextNode("My mistress when she walks, treads on the ground."));
lines.appendChild(line12);
Element line13 = doc.createElement("line");
line13.appendChild(doc.createTextNode("And yet, by Heaven, I think my love as rare"));
lines.appendChild(line13);
Element line14 = doc.createElement("line");
line14.appendChild(doc.createTextNode("As any she belied with false compare."));
lines.appendChild(line14);
root.appendChild(lines);
doc.appendChild(root);
domBuilder db = new domBuilder();
db.printDOMTree(doc);
}
catch (Exception e)
{
System.err.println(e);
}
}
}
parseString.java
这段代码展示了如何解析一个包含 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 java.io.Reader;
import java.io.StringReader;
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 org.xml.sax.InputSource;
import com.ibm.xml.parsers.*;
/**
* parseString.java
* This sample program illustrates how to parse an XML document
* contained in a String.
*/
public class parseString
{
public void parseAndPrint(InputSource xmlSource)
{
Document doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(xmlSource);
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, recursively. */
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[])
{
parseString ps = new parseString();
StringReader sr = new StringReader("<?xml version=\"1.0\"?><a>AlphaBravo<c>Charlie</c></a>");
InputSource iSrc = new InputSource(sr);
ps.parseAndPrint(iSrc);
}
}
domSorter.java
这段代码在 XML 文档中查找所有的 <line> 元素,然后排序。它展示了如何操作一个 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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.*;
/**
* domSorter.java
* This sample program illustrates how to rearrange the nodes in a
* DOM tree.
*/
public class domSorter
{
public void parseAndSortLines(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 sort it and print it.
if (doc != null)
{
sortLines(doc);
printDOMTree(doc);
}
}
public String getTextFromLine(Node lineElement)
{
StringBuffer returnString = new StringBuffer();
if (lineElement.getNodeName().equals("line"))
{
NodeList kids = lineElement.getChildNodes();
if (kids != null)
{
if (kids.item(0).getNodeType() == Node.TEXT_NODE)
{
returnString.append(kids.item(0).getNodeValue());
}
}
}
else
returnString.setLength(0);
return new String(returnString);
}
/** Sorts the <line> elements in the file.
It uses a bubble sort algorithm, since a
sonnet only has 14 lines. **/
public void sortLines(Document doc)
{
NodeList theLines = doc.getDocumentElement().
getElementsByTagName("line");
if (theLines != null)
{
int len = theLines.getLength();
for (int i = 0; i < len; i++)
for (int j = 0; j < (len - 1 - i); j++)
if (getTextFromLine(theLines.item(j)).
compareTo(getTextFromLine(theLines.item(j+1))) > 0)
theLines.item(j).getParentNode().
insertBefore(theLines.item(j+1),
theLines.item(j));
}
}
/** Prints the specified node, recursively. */
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:
{
if (node.getNodeValue().trim().length() > 0)
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();
if (data != null && data.length() > 0)
{
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 domSorter uri");
System.out.println(" where uri is the URI of the XML document you want to sort.");
System.out.println(" Sample: java domSorter sonnet.xml");
System.out.println();
System.out.println(" Note: Your XML document must use the sonnet DTD.");
System.exit(1);
}
domSorter ds = new domSorter();
ds.parseAndSortLines(argv[0]);
}
}
以下为Sun 解析器的代码
domTwo.java
这段代码等同于 domOne.java,但它使用 Sun 公司的 XML 解析器而不是 IBM 的。它展示了 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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.xml.parser.Parser;
import com.sun.xml.tree.XmlDocumentBuilder;
/**
* domTwo.java
* Illustrates how to go through a DOM tree. Identical to domOne,
* except it uses Sun抯 XML parser instead of IBM抯.
*/
public class domTwo
{
public void parseAndPrint(String uri)
{
Document doc = null;
try
{
XmlDocumentBuilder builder = new XmlDocumentBuilder();
Parser parser = new com.sun.xml.parser.Parser();
parser.setDocumentHandler(builder);
builder.setParser(parser);
builder.setDisableNamespaces(false);
parser.parse(uri);
doc = builder.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, recursively. */
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 domTwo uri");
System.out.println(" where uri is the URI of the XML document you want to print.");
System.out.println(" Sample: java domTwo sonnet.xml");
System.exit(1);
}
domTwo d2 = new domTwo();
d2.parseAndPrint(argv[0]);
}
}
saxTwo.java
这段代码等同于 saxOne.java,但它使用 Sun 公司的 XML 解析器而不是 IBM 的。它展示了 SAX 接口的可移植性。
/*
* (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.File;
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.sun.xml.parser.Resolver;
/**
* saxTwo.java
* This sample program illustrates how to use a SAX parser. It
* parses a document and writes the document contents back to
* standard output. It is identical to saxOne.java except that
* it uses Sun XML parser instead of IBM.
*/
public class saxTwo
extends HandlerBase
{
public void parseURI(String uri)
{
try
{
Parser parser = ParserFactory.makeParser();
parser.setDocumentHandler(this);
parser.setErrorHandler(this);
parser.parse(Resolver.createInputSource(new File(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 saxTwo uri");
System.out.println(" where uri is the URI of your XML document.");
System.out.println(" Sample: java saxTwo sonnet.xml");
System.exit(1);
}
saxTwo s2 = new saxTwo();
s2.parseURI(argv[0]);
}
}
|