My mistress` eyes are nothing like the sun, Coral is far more red than her lips red. If snow be white, why then her breasts are dun, If hairs be wires, black wires grow on her head. I have seen roses damasked, red and white, But no such roses see I in her cheeks. And in some perfumes is there more delight Than in the breath that from my mistress reeks. I love to hear her speak, yet well I know That music hath a far more pleasing sound. I grant I never saw a goddess go, My mistress when she walks, treads on the ground. And yet, by Heaven, I think my love as rare As any she belied with false compare.
/* * (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. */
// 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(""); 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)); }
/** 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. */
/** * 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;
// 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. */
/** * 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); } }
/** 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. */
/** * 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;
/** 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]); } }