JSP reads XML file code using SAX

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.