package cn.com.sax;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXResult;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.sax.TextElementListener;
import android.text.format.Time;
import android.util.Log;
import android.widget.TextView;
public class SaxParseXmlActivity extends Activity {
/** Called when the activity is first created. */
String result = "第二种解析方式:\n";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.txt);
TextView txt02= (TextView) findViewById(R.id.txt02);
txt.setText(saxParse());
txt02.setText(saxParseSecond());
String str = null;
try {
str = new SxaCreateXml().saxToXml(openFileOutput("sax.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 用sax解析xml的第一种方式
* @return
*/
private String saxParse(){
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser;
String xmlStr = "第一种解析方式:\n";
try {
// 读取类得包下的xml文件
// InputStream inputStream =
// this.getClass().getClassLoader().getResourceAsStream("cn/com/sax/saxTest.xml");
//读取src下xml文件
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("saxTest.xml");
// 读取assets下的xml文件
// InputStream inputStream = this.getAssets().open("saxTest.xml");
parser = factory.newSAXParser();
SaxParseHandler personHandler = new SaxParseHandler();
parser.parse(inputStream, personHandler);
xmlStr = xmlStr + personHandler.parseToString();
} catch (ParserConfigurationException e) {
e.printStackTrace();
Log.e("TEST", "" + e.toString());
} catch (SAXException e) {
e.printStackTrace();
Log.e("TEST", "" + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("TEST", "" + e.toString());
}
return xmlStr;
}
/**
* sax解析xml的第二种方式
* 用XMLReader 也是sax的一种方式
* @return
*/
private String saxParseSecond(){
//读取src下xml文件
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("saxTest.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parse = factory.newSAXParser();
XMLReader reader = parse.getXMLReader();
reader.setContentHandler(getRootElement().getContentHandler());
reader.parse(new InputSource(inputStream));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
*
* @return 返回设置好处理机制的rootElement
*/
private RootElement getRootElement(){
/*rootElement代表着根节点,参数为根节点的tagName*/
RootElement rootElement = new RootElement("classes");
/*获取一类子节点,并为其设置相应的事件
* 这里需要注意,虽然我们只设置了一次group的事件,但是我们文档中根节点下的所有
* group却都可以触发这个事件。
* */
Element groupElement = rootElement.getChild("group");
// 读到元素开始位置时触发,如读到<group>时
groupElement.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
// Log.i("TEST", "start");
String groupName = attributes.getValue("name");
String groupNum = attributes.getValue("num");
result = result+"groupName ="+groupName+"groupNum = "+groupNum+"\n";
}
});
//读到元素结束位置时触发,如读到</group>时
groupElement.setEndElementListener(new EndElementListener() {
@Override
public void end() {
}
});
Element personElement = groupElement.getChild("person");
//读取<person>标签触发
personElement.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
String personName = attributes.getValue("name");
String age = attributes.getValue("age");
result = result+"personName ="+personName+"age = "+age+"\n";
}
});
//读取</person>标签触发
personElement.setEndElementListener(new EndElementListener() {
@Override
public void end() {
}
});
Element chinese = personElement.getChild("chinese");
// chinese.setTextElementListener(new TextElementListener() {
//
// @Override
// public void end(String body) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void start(Attributes attributes) {
// // TODO Auto-generated method stub
//
// }
// });
// 读到文本的末尾时触发,这里的body即为文本的内容部分
chinese.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(body);
body = m.replaceAll("");
result = result+"chinese ="+body;
}
});
Element english = personElement.getChild("english");
english.setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String body) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(body);
body = m.replaceAll("");
result = result+"english ="+body+"\n";
}
});
return rootElement;
}
/**
* 第一种解析方式下的处理机制
*
* 2011-12-26上午09:49:20
* @author niuxuehao
*/
class SaxParseHandler extends DefaultHandler {
String result;
public String parseToString() {
return result;
}
String str = null;
/**
* xml开始时,执行此函数
*/
@Override
public void startDocument() throws SAXException {
// Log.e("TEST", "startDocument");
}
/**
* xml结束时,执行此函数
*/
@Override
public void endDocument() throws SAXException {
// Log.e("TEST", "endDocument");
}
/**
* xml标签开始时,执行此函数,读取标签名称,和标签内的属性
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if ("group".equals(localName)) {
String groupName = attributes.getValue(0);
String num = attributes.getValue(1);
result = result + "groupName=" + groupName + " num =" + num
+ "\n";
} else if ("person".equals(localName)) {
String personName = attributes.getValue(0);
String age = attributes.getValue(1);
// 和getValue(1)等效
// String txt = attributes.getValue("age");
result = result + "personName" + personName + " age=" + age
+ "\n";
} else {
str = localName;
}
// Log.e("TEST", "startElement");
}
/**
* xml标签结束时,执行此函数
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
str = null;
// Log.e("TEST", "endElement");
}
/**
* 解析标签内的值,如<chinese>sss
- 1
- 2
- 3
- 4
- 5
前往页