Package com. xml. Sax;
Import java. Io. file;
Import java. util. vector;
Import javax. xml. parsers. saxparser;
Import javax. xml. parsers. saxparserfactory;
Import org. xml. Sax. attributes;
Import org. xml. Sax. saxexception;
Import org. xml. Sax. helpers. defaulthandler;
Public class xmlcontenthandler extends defaulthandler {
/**
* @ Param ARGs
*/
Private vector <string> tagname;
Private vector <string> tagvalue;
Private int step;
// Start parsing the XML file
Public void startdocument () throws saxexception
{
Tagname = new vector <string> ();
Tagvalue = new vector <string> ();
Step = 0;
}
// End parsing the XML file
Public void enddocument () throws saxexception
{
For (INT I = 0; I <tagname. Size (); I ++)
{
If (! Tagname. Get (I). Equals ("") | tagname. Get (I )! = NULL)
{
System. Out. println ("node name:" + tagname. Get (I ));
System. Out. println ("Node value:" + tagvalue. Get (I ));
}
}
}
/**
* This method is called when a starting element is interpreted,
* What are repeated elements? Prefix attribute <name: Page LL = ""> </Name: Page> in this way, the saxexception error is thrown.
* Generally, QNAME is equal to localname.
*/
Public void startelement (string Uri, string localname, string QNAME,
Attributes attributes) throws saxexception
{
// Node name
Tagname. Add (QNAME );
// Cyclic output attributes
For (INT I = 0; I <attributes. getlength (); I ++)
{
// Get the property name
System. Out. println ("attribute name:" + attributes. getqname (I ));
// Obtain the property value
System. Out. println ("property value :"
+ Attributes. getvalue (attributes. getqname (I )));
}
}
/**
* Call this method when an end label is encountered.
*/
Public void endelement (string Uri, string localname, string QNAME)
Throws saxexception
{
Step = Step + 1;
}
/**
* Read the value in the tag. ch is used to store the XML character data of a row, including the tag. The initial size is 2048,
* Every time a new character is interpreted, it is added to Char. * Note: This char character manages stored characters by itself,
* Not every row will refresh char, start, and length once determined by the XML Element Data,
* The rule cannot be found for the time being. Check the source code later.
*
* Here is a positive label, and the anti-label will be executed once characters, so you do not need to obtain the value in the anti-label operation.
*/
Public void characters (char ch [], int start, int length)
Throws saxexception
{
// As long as the length of the current tag group is one to one, the value is not assigned, and the anti-tag is not scheduled.
If (tagname. Size ()-1 = tagvalue. Size ())
{
Tagvalue. Add (new string (CH, start, length ));
}
}
Public static void main (string [] ARGs)
{
String filename = "src/COM/XML/sax/myxml. xml ";
Saxparserfactory SPF = saxparserfactory. newinstance ();
Try
{
Saxparser = SPF. newsaxparser ();
Saxparser. parse (new file (filename), new xmlcontenthandler ());
}
Catch (exception E)
{
E. printstacktrace ();
}
}
Public vector gettagname ()
{
Return tagname;
}
Public void settagname (vector tagname)
{
This. tagname = tagname;
}
Public vector gettagvalue ()
{
Return tagvalue;
}
Public void settagvalue (vector tagvalue)
{
This. tagvalue = tagvalue;
}
}
XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<People>
<Person personid = "e01" enable = "true">
<Name> Zhang San </Name>
<Tel> 5128 </Tel>
<Email> txq512@sina.com </Email>
</Person>
<Person personid = "e02" enable = "false">
<Name> meixin </Name>
<Tel> 5252525 </Tel>
<Email> wnight88@sina.com </Email>
</Person>
<Person personid = "e03" enable = "true">
<Name> Yu </Name>
<Tel> 5389654 </Tel>
<Email> yu@188.net </Email>
</Person>
</People>