JSP tutorial parsing XML documents using JDOM reading XML files
XML file:
<?xml version= "1.0" encoding= "GB2312"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR> xx, xx Road, xx County, Sichuan Province, x section xx </ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR> xx xiang xx cun xx zu, xx City, Sichuan province </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>
<%
Jdom
Package test.xml;
Import Java.io.File;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;
/**
* Read XML files using JDOM.
*
*/
public class Testxmlbyjdom {
public static void Main (string[] args) {
Long lasting = System.currenttimemillis ();
System.out.println ("Read by JDOM");
try {
Structure
Saxbuilder builder = new Saxbuilder ();
Reading documents
Document doc = builder.build (new File ("Text.xml"));
Get root
Element foo = doc.getrootelement ();
Get subordinate Node
List Allchildren = Foo.getchildren ();
for (int i = 0; i < allchildren.size (); i++) {
System.out.print ("License plate Number:" + (Element) allchildren.get (i)). Getchild ("NO"). GetText ());
System.out.println ("Owner's Address:" + ((Element) allchildren.get (i)). Getchild ("ADDR"). GetText ());
}
catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) + "MS");
}
}
%>
</body>