xml样例,这是xpath api自带的样例
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
/**
* 使用光标的方式进行读取
* @throws XMLStreamException
* @throws IOException
*/
@Test
public void test01() throws XMLStreamException, IOException{
XMLInputFactory xf= XMLInputFactory.newInstance();
InputStream is=TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader=xf.createXMLStreamReader(is);
while(reader.hasNext()){
int type=reader.next();
if(type==XMLStreamConstants.START_ELEMENT){
System.out.println(reader.getName());
}else if (type==XMLStreamConstants.CHARACTERS){
System.out.println(reader.getText().trim());
}else if(type==XMLStreamConstants.END_ELEMENT){
System.out.println(reader.getName());
}
}
if(is!=null){
is.close();
}
}