How to use the dom4j of the JSP tutorial to read parse XML files
<?xml version= "1.0" encoding= "GB2312"?>
<RESULT>
<VALUE>
<NO>111cn.net</NO>
<ADDR> China web first stop </ADDR>
</VALUE>
</RESULT>
<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<body>
<%
Use dom4j to read XML files.
dom4j
Package test.xml;
Import Java.io.File;
Import Java.util.Iterator;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
public class Testxmlbydom4j {
public static void Main (string[] args) {
Long lasting = System.currenttimemillis ();
System.out.println ("Read by dom4j");
try {
File F = new file ("Text.xml");
Parser
Saxreader reader = new Saxreader ();
Reading and parsing files
Document doc = Reader.read (f);
Root node
Element root = Doc.getrootelement ();
Element foo;
Child node Traversal
for (Iterator i = root.elementiterator ("VALUE"); I.hasnext ();) {
Foo = (Element) i.next ();
System.out.print ("License plate number:" + foo.elementtext ("NO"));
System.out.println ("Owner address:" + foo.elementtext ("ADDR"));
}
catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) + "MS");
}
}
%>
</body>