//使用过滤器
@Test
public void test05() throws XMLStreamException, IOException{
XMLInputFactory xf= XMLInputFactory.newInstance();
InputStream is=TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//创建过滤器
XMLEventReader reader=xf.createFilteredReader(xf.createXMLEventReader(is), new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
if(event.isStartElement()){
return true;
}
return false;
}
});
while(reader.hasNext()){
XMLEvent event=reader.nextEvent();
if(event.isStartElement()){
String name = event.asStartElement().getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
}
if(is!=null){
is.close();
}
}
@Test
public void test05() throws XMLStreamException, IOException{
XMLInputFactory xf= XMLInputFactory.newInstance();
InputStream is=TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//创建过滤器
XMLEventReader reader=xf.createFilteredReader(xf.createXMLEventReader(is), new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
if(event.isStartElement()){
return true;
}
return false;
}
});
while(reader.hasNext()){
XMLEvent event=reader.nextEvent();
if(event.isStartElement()){
String name = event.asStartElement().getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
}
if(is!=null){
is.close();
}
}