Example of parsing XML files using Java

Source: Internet
Author: User

XML file

<? XML version = "1.0" encoding = "gb2312"?>
<Person>
<Name> ssuupv </Name>
<College> School of materials </College>
<Telephone> 6258113 </telephone>
<Notes> male, born in 1979, undergraduate, transferred to china Ceramics University in 99 </Notes>
</Person>

Java code

Import java. Io .*;
Import java. util. hashtable;
Import org. xml. Sax .*;

Public class saxhandler extends handlerbase {
Private hashtable table = new hashtable ();

Private string currentelement = NULL;

Private string currentvalue = NULL;

Public void settable (hashtable table ){
This. Table = table;
}

Public hashtable gettable (){
Return table;
}

Public void startelement (string tag, attributelist attrs) throws saxexception {
Currentelement = tag;
}

Public void characters (char [] CH, int start, int length) throws saxexception {
Currentvalue = new string (CH, start, length );
}

Public void endelement (string name) throws saxexception {
If (currentelement. Equals (name ))
Table. Put (currentelement, currentvalue );
}
}

 

JSP file

<HTML>

<Head>

<Title> analyze the XML file people. xml </title>

</Head>

<Body>

<% @ Page errorpage = "errpage. jsp"

Contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<% @ Page import = "Java. util. hashtable" %>

<% @ Page import = "org. W3C. Dom. *" %>

<% @ Page import = "org. xml. Sax. *" %>

<% @ Page import = "javax. xml. parsers. saxparserfactory" %>

<% @ Page import = "javax. xml. parsers. saxparser" %>

<%
System. Out. println (">>>>>>>>>>>>>>>>>>>>>>>>> ");
File file = new file ("D: \ projects \ test \ webapp \ people. xml ");
Filereader reader = new filereader (File );
// String reader = "<person> <Name> ssuupv </Name> <College> School of materials </College> <telephone> 6258113 </telephone> <Notes> male, born in 1979 bachelor's Degree transferred to Emy of ceramics in 79 years </Notes> </person> ";

Parser;

Saxparserfactory SPF = saxparserfactory. newinstance ();

Saxparser sp = SPF. newsaxparser ();

Com. Test. saxhandler handler = new COM. Test. saxhandler. saxhandler ();

Sp. parse (New inputsource (Reader), Handler );

Hashtable = handler. gettable ();

Out. println ("<Table border = 2> <caption> instructor information table </caption> ");

Out. println ("<tr> <TD> name </TD>" + "<TD>" +

(String) hashtable. Get (new string ("name") + "</TD> </tr> ");

Out. println ("<tr> <TD> school </TD>" + "<TD>" +

(String) hashtable. Get (new string ("college") + "</TD> </tr> ");

Out. println ("<tr> <TD> phone number </TD>" + "<TD>" +

(String) hashtable. Get (new string ("telephone") + "</TD> </tr> ");

Out. println ("<tr> <TD> remarks </TD>" + "<TD>" +

(String) hashtable. Get (new string ("Notes") + "</TD> </tr> ");

Out. println ("</table> ");

%>

</Body>

</Html>

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.