SAXParser 解析器和 XMLEventReader(读取XML文档)



import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
//这个SAXParser只能按顺序进行调用不能返回重复调用函数进行解析,
public class SAXDemo{
@Test
public void demo() throws Exception {
//1.获得SAXParser的工厂类
SAXParserFactory factory=SAXParserFactory.newInstance();
//2.获得SAXParser解析器
SAXParser parse=factory.newSAXParser();
//获得XMLReader对象
XMLReader read=parse.getXMLReader();
//这里是设置监听,要覆盖原来的函数就按shif+alt+s再按V再从里面进行选择
read.setContentHandler(new DefaultHandler(){
private String elementName="";

@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
if(qName.equals("user")){
String id=attributes.getValue("id");
System.out.println("id="+id);
}
else if(qName.equals("name")||qName.equals("age")){
elementName=qName;
}

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("name")||qName.equals("age")){
elementName="";
}
if(qName.equals("user")){
System.out.println("-------------------------");}

}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(!elementName.equals("")){
String str=new String(ch,start,length);
System.out.println(elementName+":"+str);
}
}

});
read.parse("./xml/users.xml");
}
}

-----------------------------------------------------------------------------------

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.junit.Test;
import org.xml.sax.XMLReader;


public class STAdemo {
@Test
public void demo() throws Exception {
XMLInputFactory factory=XMLInputFactory.newInstance();
XMLEventReader read=factory.createXMLEventReader(new FileReader("./xml/users.xml"));
//这里跟SAXParser的差别在于XMLEventReader是有迭代器的它可以往上一级访问,而SAX不能只能一次惊醒访问,所以XML比SAX
//更加方便查询xml;
while(read.hasNext()){
XMLEvent en=read.nextEvent();
if(en.isStartElement()){
StartElement se=en.asStartElement();
if(se.getName().getLocalPart().equals("user")){
String id=se.getAttributeByName(new QName("id")).getValue();
System.out.println("ID="+id);
}
if(se.getName().getLocalPart().equals("name")){
Characters ch= read.nextEvent().asCharacters();
System.out.println("Name="+ch.toString());
}
if(se.getName().getLocalPart().equals("age")){
Characters chs=read.nextEvent().asCharacters();
System.out.println("age="+chs.toString());
}
}
if(en.isEndElement()){
if(en.asEndElement().getName().getLocalPart().equals("user")){
System.out.println("-------------------------");
}
}
}

}

}

posted @ 2016-09-23 14:49  折腾青春  阅读(2311)  评论(0编辑  收藏  举报