In Java programming, we often use XML to store data. To obtain data in XML, We need to parse XML. The following is an example of using Sax to parse XML,
General steps:
1. Create a saxparserfactory object; saxparserfactory SPF = saxparserfactory. newinstance ();
2. Use the factory object created above to create a saxparser resolution object; saxparser sp = SPF. newsaxparser ();
3. Create a saxhandler processor. The saxhandler class inherits defaulthandler and re-writes the method. The main methods include public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {} is triggered when the node element of the XML data is read. You need to perform this operation to mark the element name. Public void characters (char [] CH, int start, int length) throws saxexception {} This method can process data between nodes; Public void endelement (string Uri, string ocalname, string QNAME) throws saxexcep Tion {} triggered when the processing node element ends. You can add code to store node data.
The following is a specific parsing example:
Person. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Persons>
<Name> ciacs </Name>
<Address>
<Country> China </country>
<Province> Guangdong </province>
</Address>
<Cnblogs> http://www.cnblogs.com/zhi-hao/ </cnblogs>
</Persons>
Sax. Java
Import java. Io. file;
Import java. Io. filereader;
Import java. util. hashtable;
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;
Public class sax {
Public static void main (string [] ARGs)
{
Try {
File file = new file ("person. xml ");
Filereader reader = new filereader (File );
Saxparserfactory SPF = saxparserfactory. newinstance (); // create saxparserfactory
Saxparser sp = SPF. newsaxparser (); // create saxparser
Saxhandler handler = new saxhandler (); // create a saxhandler processor that inherits the defaulthandler implementation.
Sp. parse (New inputsource (Reader), Handler); // Parse XML
Hashtable table = handler. gettable ();
// Output XML data
System. Out. println ("name:" + table. Get ("name "));
System. Out. println ("adderss :");
System. Out. println ("Country:" + table. Get ("country") + "province:" + table. Get ("Province "));
System. Out. println ("cnblogs:" + table. Get ("cnblogs "));
}
Catch (exception E)
{E. printstacktrace ();}
}
}
// Implement your own defined saxhandler class
Class saxhandler extends defaulthandler
{
Private string currentelement;
Private string currentvalue;
Private hashtable table = new hashtable (); // store data in XML
@ Override public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception
{
Currentelement = QNAME;
}
@ Override public void characters (char [] CH, int start, int length) throws saxexception
{
Currentvalue = new string (CH, start, length );
}
@ Override public void endelement (string Uri, string localname, string QNAME) throws saxexception
{
If (currentelement. Equals (QNAME ))
{
Table. Put (currentelement, currentvalue); // put the data in XML into table
}
}
Public hashtable gettable ()
{
Return table;
}
}
Output result
Learn together and make progress together!