📄 domtwo.java
字号:
/*
* (C) Copyright IBM Corp. 2003. 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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree. Unlike DOMOne, it doesn't include
* whitespace.
*/
public class DomTwo
{
public void parseAndPrint(String uri)
{
Document doc = null;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(uri);
if (doc != null)
printDomTree(doc);
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
}
/** 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++)
printDomTree(attrs.item(i));
System.out.print(">");
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDomTree(children.item(i));
}
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
break;
}
// Print attribute nodes
case Node.ATTRIBUTE_NODE:
{
String value = ((Attr)node).getValue();
System.out.print(" " + node.getNodeName() +
"=\"" + value + "\"");
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());
System.out.print(" ");
System.out.print(node.getNodeValue());
System.out.print("?>");
break;
}
// print a comment
case Node.COMMENT_NODE:
{
System.out.print("<!-- ");
System.out.print(node.getNodeValue());
System.out.print("-->");
}
}
} // printDomTree(Node)
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0 ||
(argv.length == 1 && argv[0].equals("-help")))
{
System.out.println("\nUsage: 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.out.println("\nParses an XML document, then writes the DOM " +
"tree to the console.");
System.exit(1);
}
DomTwo d2 = new DomTwo();
d2.parseAndPrint(argv[0]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -