In this article, we use SAX to parse XML. In Android, we can use SAX and DOM. DOM needs to read the entire XML file into the memory and parse it, which consumes a lot of memory. However, SAX is based on event-driven processing methods, callback functions can be triggered on each node. However, SAX is suitable for XML documents with simple node structures. Complex XML documents may be difficult to process in the future.
The test. xml file to be parsed in this article is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<Test>
<Title> testSAX </title>
<Content aa = "1" bb = "2">
<Name> hellogv </name>
<Url> http://blog.csdn.net/hellogv </url>
</Content>
</Test>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Test>
<Title> testSAX </title>
<Content aa = "1" bb = "2">
<Name> hellogv </name>
<Url> http://blog.csdn.net/hellogv </url>
</Content>
</Test>
The result of parsing the above XML is as follows:
To use SAX parsing, You need to define SAXParserFactory (so that the application can configure and obtain a SAX-based parser to parse XML documents), SAXParser (Parse XML from various input sources ), XMLReader (read XML documents using callback functions), among which XMLReader is critical. XMLReader can define various callback functions for parsing XML. These callback functions are triggered when the conditions are met.
View plaincopy to clipboardprint?
SAXParserFactory factory = SAXParserFactory. newInstance ();
SAXParser parser = factory. newSAXParser ();
XMLReader reader = parser. getXMLReader ();
Reader. setContentHandler (handler );
Reader. parse (new InputSource (testSAX. this. getResources ()
. OpenRawResource (R. raw. test )));
SAXParserFactory factory = SAXParserFactory. newInstance ();
SAXParser parser = factory. newSAXParser ();
XMLReader reader = parser. getXMLReader ();
Reader. setContentHandler (handler );
Reader. parse (new InputSource (testSAX. this. getResources ()
. OpenRawResource (R. raw. test )));
In this Code, XMLReader calls the SAXHandler that inherits DefaultHandler. DefaultHandler has implemented ContentHandler, DTDHandler, EntityResolver, ErrorHandler and Other interfaces, including common XML reading operations. For details, see the following SAXHandler. java source code.
The XML generation result is as follows:
After reading each node, use XmlSerializer to re-combine and output the XML string.
The main. xml code in this article is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/btnSAX"
Android: text = "using SAX to parse XML"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "generate XML" android: id = "@ + id/btnOutput"> </Button>
<EditText android: text = "@ + id/EditText01" android: id = "@ + id/EditText01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </EditText>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: id = "@ + id/btnSAX"
Android: text = "using SAX to parse XML"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "generate XML" android: id = "@ + id/btnOutput"> </Button>
<EditText android: text = "@ + id/EditText01" android: id = "@ + id/EditText01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </EditText>
</LinearLayout>
The source code of SAXHandler. java is as follows:
View plaincopy to clipboardprint?
Package com. testSAX;
Import java. util. ArrayList;
Import org. xml. sax. Attributes;
Import org. xml. sax. SAXException;
Import org. xml. sax. helpers. DefaultHandler;
Import android. util. Log;
Public class SAXHandler extends DefaultHandler {
Private ArrayList <String> keys = new ArrayList <String> (); // Save the field name.
Private ArrayList <Object> values = new ArrayList <Object> (); // Save the value
@ Override
Public void startDocument () throws SAXException {
Super. startDocument ();
}
@ Override
Public void endDocument () throws SAXException {
Super. endDocument ();
}
@ Override
Public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// Save the Start mark
Keys. add ("startTag ");
Values. add (localName );
Log. e ("startTag", localName );
// Save the property value
For (int I = 0; I <attributes. getLength (); I ++ ){
Keys. add ("Attr ");
String [] str = new String [2];
& Nb