import java.io.File;
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 javax.xml.parsers.*;
//import com.ibm.xml.parsers.*;/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/public class domOne
{
public void parseAndPrint(String uri)
{
Document doc = null; try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(uri));
}
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.print(">"); 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.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
} // printDOMTree(Node) /** 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]);
}
}
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 javax.xml.parsers.*;
//import com.ibm.xml.parsers.*;/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/public class domOne
{
public void parseAndPrint(String uri)
{
Document doc = null; try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(uri));
}
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.print(">"); 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.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
} // printDOMTree(Node) /** 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]);
}
}
解决方案 »
- 求解:String长度为什么不可变?
- hibernate中query.list()出现重复值?
- 刚学习hibernate,出现的错误,请大侠们解决哈
- 数据库和网页上的字符编码不一致会引起什么问题?
- 高分求助Spring rmi问题!!!!
- 关于jsp的forward和request.getRequestURL()的疑惑,请达人来解决一下
- 请教高手jbuilder支持工作流技术吗?如xlang ,wsfl ,bpws等。
- jboss部署EJB是否一定要上网?
- 帮忙出主意!!!头大!!!
- j2ee1.3平台上的EJB,home文件问题,请大家帮忙!!!!!
- 答谢所有支持CSDN的朋友们,散分!
- weblogic7.0+SQL Server2000部署应用程序的问题,急!
1>包是jdk自带的,不用下载结果><?xml version="1.0" ?>
<person>
<name>
vvgg
</name>
<add>
jiangsu china
</add>
<age>
22
</age>
</person>