Java XML parsing-using the sax example

Source: Internet
Author: User

Important Note (the XML parsing by Sax is based on the event model, that is, the message mechanism .) :

Defaulthandler class (Org. xml. Sax. helpers. defaulthandler), java. Io. chararraywriter class, javax. xml. parsers. saxparserfactory class, javax. xml. parsers. saxparser class

Functions:

Public void startdocument () throws saxexception;

Public void enddocument () throws saxexception;

Public void startelement (string namesapaceuri, string localname, string QNAME, attributes ATTR) throws saxexception;

Public void endelement (string namespaceuri, string localname, string QNAME) throws saxexception;

Public void characters (char [] CH, int start, int length) throws saxexception;

The callback methods include startdocument, enddocument, startelement, endelement, and characters.

The functions are called when the document is accessed, when the document is accessed, when the element is accessed, and when a string cannot be used as a tag (that is, a string similar to <mytag> hello, I am Alex! </Mytag> in "Hello, I am Alex! ).
In addition, note: to create a parsing XML class, how to bind the custom class to "defaulhandler" (saxparser. parse (new file ("ticketrequest. XML "), TRP );).

In fact, although the following program is very simple, it is indeed the basic framework for parsing XML using the sax method.

// Ticketreuqestparser. Java

Package myjaxp;

Import myjaxp. ticketrequest2;
Import org. xml. Sax .*;
Import org. xml. Sax. helpers .*;
Import javax. xml. parsers .*;
Import java. Io .*;
Public class ticketreuqestparser extends defaulthandler
{
Private chararraywriter buffer = new chararraywriter ();
Static ticketrequest2 tr2;
String custidstring;
String cruiseidstring;
Public void startdocument () throws saxexception
{
System. Out. println ("startdocument () called ");
}
Public void enddocument () throws saxexception
{
System. Out. println ("enddocument () called ");
}
Public void startelement (string namesapaceuri, string localname, string QNAME, attributes ATTR) throws saxexception
{

For (INT I = 0; I <ATTR. getlength (); I ++)
{
String attrname = ATTR. getqname (I );
If (attrname. Equals ("custid "))
{
Custidstring = ATTR. getvalue (I );
}
If (attrname. Equals ("cruiseid "))
{
Cruiseidstring = ATTR. getvalue (I );
}
}
Buffer. Reset ();
}
Public void endelement (string namespaceuri, string localname, string QNAME) throws saxexception
{
// System. Out. println ("endelement called ");
Storeelement (QNAME );
}
Public void characters (char [] CH, int start, int length) throws saxexception
{
Buffer. Write (CH, start, length );
}
Private void storeelement (string elementname)
{
If (elementname. Equals ("ticketrequest "))
{

}
If (elementname. Equals ("customer "))
{
Tr2.setcustid (integer. parseint (custidstring ));
}
If (elementname. Equals ("firstname "))
{
Tr2.setfirstname (buffer. tostring ());
}
If (elementname. Equals ("lastname "))
{
Tr2.setlastname (buffer. tostring ());
}
If (elementname. Equals ("Cruise ")){
Tr2.setcruiseid (integer. parseint (cruiseidstring ));
}
If (elementname. Equals ("destination "))
{
Tr2.setdestination (buffer. tostring ());

}
If (elementname. Equals ("Port "))
{
Tr2.setport (buffer. tostring ());
}
If (elementname. Equals ("Sailing "))
{
Tr2.setsailing (buffer. tostring ());
}
If (elementname. Equals ("numberoftickets "))
{
String numberofticketstring = buffer. tostring ();
Int numberoftickets;
If (! Numberofticketstring. Equals (""))
{
Numberoftickets = integer. parseint (numberofticketstring );

Tr2.setnumberoftickets (numberoftickets );}
}
If (elementname. Equals ("iscommissionable "))
{
Tr2.setcommissionable (true );
}

}
Public String tostring ()
{
Return tr2.tostring ();
}
Public static void main (string [] ARGs)
{
System. Out. println ("ticketrequestparser main ()");
Defaulthandler TRP = new ticketreuqestparser ();
Tr2 = new ticketrequest2 ();
Saxparserfactory factory = saxparserfactory. newinstance ();
Try
{
Saxparser = factory. newsaxparser ();
Saxparser. parse (new file ("ticketrequest. xml"), TRP );
}
Catch (exception E)
{
System. Out. println ("exception in:" + E );
}
System. Out. println (TRP );
}
 
}
 Appendix:

/*
* Ticketrequest2.java
*
* Created on January 19,200 2, AM
*/

Package myjaxp;

/**
*
* @ Author Stephen Potts
* @ Version
*/
Public class ticketrequest2 implements java. Io. serializable
{
// Information about the customer
Int custid;
String lastname;
String firstname;

// Information about the cruise
Int cruiseid;
String destination;
String port;
String sailing;

Int numberoftickets;
Boolean commissionable;
Boolean approved;

Public ticketrequest2 ()
{
}

/** Constructor */
Public ticketrequest2 (INT custid, string lastname, string firstname,
Int cruiseid, string destination,
String port, string sailing, int numberoftickets,
Boolean iscommissionable)
{
// Set the information about the customer
This. custid = custid;
This. lastname = lastname;
This. firstname = firstname;

// Set the information about the cruise
This. cruiseid = cruiseid;
This. Destination = destination;
This. Port = port;
This. Sailing = sailing;

This. numberoftickets = numberoftickets;
This. commissionable = commissionable;
This. Approved = false;
}

Public int getcustid ()
{
Return this. custid = custid;
}

Public String getlastname ()
{
Return this. lastname = lastname;
}

Public String getfirstname ()
{
Return this. firstname = firstname;
}

Public int getcruiseid ()
{
Return this. cruiseid;
}

Public String getdestination ()
{
Return this. destination;
}

Public String getport ()
{
Return this. port;
}

Public String getsailing ()
{
Return this. Sailing;
}

Public int getnumberoftickets ()
{
Return this. numberoftickets;
}

Public Boolean iscommissionable ()
{
Return this. commissionable;
}

Public Boolean isapproved ()
{
Return approved;
}

Public void setcustid (INT custid)
{
This. custid = custid;
}

Public void setlastname (string lastname)
{
This. lastname = lastname;
}

Public void setfirstname (string firstname)
{
This. firstname = firstname;
}

Public void setcruiseid (INT cruiseid)
{
This. cruiseid = cruiseid;
}

Public void setdestination (string destination)
{
This. Destination = destination;
}

Public void setport (string port)
{
This. Port = port;
}

Public void setsailing (string sailing)
{
This. Sailing = sailing;
}

Public void setnumberoftickets (INT numberoftickets)
{
This. numberoftickets = numberoftickets;
}

Public void setcommissionable (Boolean commissionable)
{
This. commissionable = commissionable;
}

Public void approve ()
{
Approved = true;
}

Public void disapprove ()
{
Approved = false;
}

Public String tostring ()
{
String outstring;
Outstring = "-----------------------------------------" + "/N ";

// Information about the customer
Outstring + = "custid =" + this. custid + "/N ";
Outstring + = "lastname =" + this. lastname + "/N ";
Outstring + = "firstname =" + this. firstname + "/N ";
Outstring + = "---------------------------------------" + "/N ";

// Information about the cruise
Outstring + = "cruiseid =" + this. cruiseid + "/N ";
Outstring + = "Destination =" + this. Destination + "/N ";
Outstring + = "Port =" + this. Port + "/N ";
Outstring + = "Sailing =" + this. Sailing + "/N ";
Outstring + = "numberoftickets =" + this. numberoftickets + "/N ";

// Information about the status
Outstring + = "commissionable =" + this. commissionable + "/N ";
Outstring + = "approved? = "+ This. Approved +"/N ";
Outstring + = "---------------------------------------" + "/N ";
Return outstring;
}
}

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////

Ticketrequest. xml

<? XML version = '1. 0' encoding = 'utf-8' standalone = 'no'?>
<! -- This XML document represents a request for a cruise ticket -->

<! Doctype ticketrequest System "ticketrequest. DTD">

<Ticketrequest>
<Customer custid = "10003">
<Lastname> Carter </lastname>
<Firstname> Joseph </firstname>
</Customer>
<Cruise cruiseid = "3004">
<Destination> Hawaii </destination>
<Port> Honolulu </port>
<Sailing> 7/7/2001 </Sailing>
<Numberoftickets> 5 </numberoftickets>
<Iscommissionable/>
</Cruise>
</Ticketrequest>

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////

Ticketrequest. DTD

<! Element ticketrequest (customer, cruise)>
<! Element customer (lastname, firstname)>
<! ATTLIST customer custid nmtoken # required>
<! Element lastname (# pcdata)>
<! Element firstname (# pcdata)>
<! Element cruise (destination, port, sailing, numberoftickets, iscommissionable?)>
<! ATTLIST cruise cruiseid nmtoken # required>
<! Element destination (# pcdata)>
<! Element port (# pcdata)>
<! Element sailing (# pcdata)>
<! Element numberoftickets (# pcdata)>
<! Element iscommissionable empty>

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.