JSP Tutorial reads XML file code using SAX
<%@ 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 "/>
<body>
<%
Sax
Package test.xml;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
/**
* Read XML files using SAX.
*
*/
public class Testxmlbysax extends DefaultHandler {
Java.util.Stack tags = new java.util.Stack ();
public static void Main (String args[]) {
long lasting = System.currenttimemillis ();
& nbsp; System.out.println ("Read by SAX");
try {
//Build Factory
SAXParserFactory SF = Saxparserfactory.newinstance ();
SAXParser sp = Sf.newsaxparser ();
//Build process class, each tag will parse the processing class
testxmlbysax reader = New Testxmlbysax ();
//parsing
sp.parse (New InputSource ("Text.xml"), Reader);
} catch (Exception e) {
e.printstacktrace ();
}
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) + "milliseconds");
& nbsp }
@Override
public void characters (char ch[], int start, int length) throws Saxexception {
String tag = (string) tags.peek ();
if (Tag.equals ("NO")) {
SYSTEM.OUT.PRINTLN ("License plate number:" + length + new String (CH, start, length));
}
if (Tag.equals ("ADDR")) {
SYSTEM.OUT.PRINTLN ("Address:" + length + new String (CH, start, length));
}
}
@SuppressWarnings ("Unchecked")
@Override
public void Startelement (string uri, String localname, String qName, Attributes attrs) {
Tags.push (QName);
}
}
%>
</body>
XML file:
<?xml version= "1.0" encoding= "GB2312"
<result>
<VALUE>
& nbsp; <NO>A1234</NO>
<ADDR> xx County, xx town xx Road, Sichuan xx </addr>
</value>
<VALUE>
<NO>B1234</NO>
<ADDR> Sichuan xx xx village xx Group </ Addr>
</value>
</result>