This article uses sax to parse XML, and in Android you can use sax and dom,dom to read the entire XML file into memory and then parse it to consume memory, and sax can trigger callback functions at each node based on event-driven processing, However, sax is suitable for XML documents with simple node structure, and complex XML documents have a bit of trouble in later node depth processing.
The Test.xml files to be resolved in this article are as follows:
<?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>
Using SAX parsing, you need to define SAXPARSERFACTORY (enabling applications to configure and get a SAX based parser to parse XML documents), SAXParser (parsing XML from various input sources), XMLReader (using callback functions to read XML documents), Among them XmlReader is a key. XmlReader can define various callback functions for parsing XML, triggering these callback functions when conditions are met.
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 invokes the Saxhandler that inherits DefaultHandler. DefaultHandler has implemented interfaces such as ContentHandler, Dtdhandler, Entityresolver, and ErrorHandler, containing common XML-reading operations, Please see the following Saxhandler.java source code specifically.
After reading each node, use XmlSerializer to regroup and output the XML string. The Main.xml code for this article is as follows:
<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 android:layout_height=" wrap_content "
android:layout_width=" Fill_parent " android:text= "Generate XML" android:id= "@+id/btnoutput" >
<edittext android:text= "@+id/edittext01" @+id/edittext01 "
android:layout_width=" fill_parent "android:layout_height=" Fill_parent ">
Testsax.java source code is as follows:
Package com.testsax;
Import Java.io.StringWriter;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import Org.xml.sax.InputSource;
Import Org.xml.sax.XMLReader;
Import Org.xmlpull.v1.XmlSerializer;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.util.Xml;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText; The public class Testsax extends activity {/** called the ' when ' is the ' The activity ' is a-a-created, btnsax
;
EditText Memo;
Saxhandler handler = new Saxhandler ();
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Btnsax = (Button) This.findviewbyid (r.id.btnsax);
Btnsax.setonclicklistener (New Clickevent ());
Btnoutput = (Button) This.findviewbyid (r.id.btnoutput);
Btnoutput.setonclicklistener (New Clickevent ()); Memo = (EdittexT) This.findviewbyid (R.ID.EDITTEXT01);
Class Clickevent implements View.onclicklistener {@Override public void OnClick (View v) { if (v = = btnsax) {//parse XML, and save tag, attribute equivalent try {saxparserfactory factory = Saxparserfa
Ctory.newinstance ();
SAXParser parser = Factory.newsaxparser ();
XMLReader reader = Parser.getxmlreader ();
Reader.setcontenthandler (handler);
Reader.parse (New InputSource (TestSAX.this.getResources (). Openrawresource (R.raw.test)));
catch (Exception ee) {} ' else if (v = = btnoutput) {//Generate XML try {
XmlSerializer serializer = Xml.newserializer ();
StringWriter writer = new StringWriter ();
try {serializer.setoutput (writer); Serializer.startDocument ("UTF-8", true); for (int I=0;i
above is the whole content of this article, I hope to help you learn.