使用stax操作xml(1)
2014-05-12 16:39
239 查看
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();
}
}
<?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();
}
}
相关文章推荐
- 使用stax操作xml(2)
- 使用stax操作xml(3)
- 使用JDK自带的Stax操作XML
- 使用stax操作xml(4)
- 使用stax操作xml(5)
- 使用stax操作xml(6)
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- 详解PHP使用DOMDocument类遍历、增加、修改、删除XML节点操作
- 1 详解PHP使用DOMDocument类遍历、增加、修改、删除XML节点操作
- 使用dom4j操作xml实现增删改查
- 使用xpath操作xml(一.入门知识+xpath核心函数库 )
- C#操作Xml:使用XmlWriter写Xml
- 使用C#对XML进行增删改查操作
- 使用Dom4j操作XML
- 使用 Pull 解析器操作 XML 文件
- 使用 StAX 解析 XML
- 使用Stax解析XML
- Python使用XML操作mapnik,实现复杂标注(Multi line text symbolizer)
- 使用MyBatis对表执行CRUD操作——基于XML的实现
- 使用jdom操作xml数据,生成含Jtree的applet(转载 Jagie 原创 )