- 相關(guān)推薦
實(shí)現(xiàn)xml文件解析三種方式
1.sax方式
復(fù)制代碼 代碼如下:
/**
* 使用sax解析
*/
public class SaxParse{
/**
* sax解析器
*/
private SAXParser parser;
public SaxParse(){
try {
SAXParserFactory f = SAXParserFactory.newInstance();
parser = f.newSAXParser();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public ListdoParse(InputStream is) {
try {
XmlHandler h = new XmlHandler();
parser.parse(is,h);
return h.getpersons();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 處理器
*/
class XmlHandler extends DefaultHandler{
Listpersons = null ;
Person person = null ;
//當(dāng)前元素名稱(chēng)
private String currEleName;
/**
* 文本節(jié)點(diǎn)觸發(fā)該方法
*/
public void characters(char[] ch, int start, int length)throws SAXException {
String str = new String(ch,start,length);
//name
if("name".equals(currEleName)){
person.name = str ;
}
else if("age".equals(currEleName)){
person.age = Integer.parseInt(str);
}
}
public void endDocument() throws SAXException {
}
/**
* 元素結(jié)束
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
}
//將當(dāng)前元素置空
else if(("name".equals(currEleName)) || ("age".equals(currEleName))){
this.currEleName = "" ;
}
}
/**
* 文檔開(kāi)始事件
*/
public void startDocument() throws SAXException {
persons = new ArrayList();
}
/**
* 元素開(kāi)始事件
* localName:本地名
* uri:名字空間
* qName:限定名,前綴 + 本地名
*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//實(shí)例化person對(duì)象
if("person".equals(localName)){
person = new Person();
person.id = Integer.parseInt(attributes.getValue(0));
}
//name元素
else if("name".equals(localName)){
this.currEleName = "name" ;
}
//name元素
else if("age".equals(localName)){
this.currEleName = "age" ;
}
}
public Listgetpersons(){
return persons ;
}
}
}
2.dom方式
復(fù)制代碼 代碼如下:
/**
* DOM解析
*/
public class DomParse{
//
private DocumentBuilder builder;
public DomParse(){
try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
this.builder = f.newDocumentBuilder();
} catch (Exception e) {
e.printStackTrace();
}
}
public ListdoParse(InputStream is) {
Listpersons = new ArrayList();
Person person = null ;
try {
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("person");
Element ele = null ;
for(int i = 0 ; i < list.getLength() ; i ++){
ele = (Element) list.item(i);
person = new Person();
person.id = Integer.parseInt(ele.getAttribute("id"));
person.name = getSubElementTextContent(ele,"name");
person.age = Integer.parseInt(getSubElementTextContent(ele,"age"));
persons.add(person);
}
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
/**
* 得到指定的資源中中間的文本內(nèi)容
*/
private String getSubElementTextContent(Element ele, String tagName) {
NodeList list = ele.getElementsByTagName(tagName);
Element e = (Element) list.item(0);
//得到中間的文本節(jié)點(diǎn)
return e.getTextContent();
}
}
3.pull方式
復(fù)制代碼 代碼如下:
/**
* pull解析,拉模式,可以手動(dòng)控制下一個(gè)事件是否觸發(fā).
*/
public class PullParse{
public ListdoParse(InputStream is) {
Listpersons = null ;
Person person = null ;
try {
XmlPullParser parser = Xml.newPullParser();
//設(shè)置解析數(shù)據(jù)源
parser.setInput(is, "utf-8");
//取得事件的類(lèi)型
int eventType = parser.getEventType();
String eleName = null ;
while(eventType != XmlPullParser.END_DOCUMENT){
switch(eventType){
//文檔開(kāi)始
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList();
break ;
//元素開(kāi)始
case XmlPullParser.START_TAG:
eleName = parser.getName();
if("person".equals(eleName)){
person = new Person();
person.id = Integer.parseInt(parser.getAttributeValue(0));
}
else if("name".equals(eleName)){
person.name = parser.nextText();
}
else if("age".equals(eleName)){
person.age = Integer.parseInt(parser.nextText());
}
break ;
//標(biāo)記結(jié)束
case XmlPullParser.END_TAG:
eleName = parser.getName();
if("person".equals(eleName)){
persons.add(person);
}
break ;
}
//手動(dòng)激活下個(gè)事件的觸發(fā)
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
}
【實(shí)現(xiàn)xml文件解析三種方式】相關(guān)文章:
Java多線(xiàn)程的實(shí)現(xiàn)方式07-08
php解析ini配置文件07-13
新西蘭留學(xué)住宿的三種方式07-01
dos實(shí)現(xiàn)文件夾樹(shù)目錄生成09-16
SAT語(yǔ)法考試的出題方式解析01-20
網(wǎng)站優(yōu)化要實(shí)現(xiàn)內(nèi)容的聚合的方式10-01
高爾夫三種基本握桿方式06-10
愛(ài)爾蘭獎(jiǎng)學(xué)金發(fā)放的三種方式08-09
寶寶表達(dá)情感需求的三種方式08-20