STAX
Stax不是采取Dom4j这种全部读取的方式,而是像PULL、Sax的ContentHandler这种一个节点一个节点读取的方式,提供了基于光标的方式、迭代的方式、XPath的方式。这种处理方式的基础就是把XML文档看做一个一个的节点组成:ElementNode、TextNode等节点,每读取到一个节点就生成一个事件或者Element,在XMLStreamConstants中定义。
<?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>
public class TestStax {
@Test
publicvoid test01() {
XMLInputFactoryfactory = XMLInputFactory.newInstance();
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReaderreader = factory.createXMLStreamReader(is);
while(reader.hasNext()){
int type = reader.next();
//判断节点类型是否是开始或者结束或者文本节点,之后根据情况及进行处理
if(type==XMLStreamConstants.START_ELEMENT){
System.out.println(reader.getName());
} elseif(type==XMLStreamConstants.CHARACTERS) {
System.out.println(reader.getText().trim());
} elseif(type==XMLStreamConstants.END_ELEMENT) {
System.out.println("/"+reader.getName());
}
}
}catch (XMLStreamException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
publicvoid test02() {
XMLInputFactoryfactory = XMLInputFactory.newInstance();
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReaderreader = factory.createXMLStreamReader(is);
while(reader.hasNext()){
inttype = reader.next();
if(type==XMLStreamConstants.START_ELEMENT){
String name =reader.getName().toString();
if(name.equals("book")){
System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
}
}
}
}catch (XMLStreamException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
publicvoid test03() {
XMLInputFactoryfactory = XMLInputFactory.newInstance();
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReaderreader = factory.createXMLStreamReader(is);
while(reader.hasNext()){
inttype = reader.next();
if(type==XMLStreamConstants.START_ELEMENT){
String name =reader.getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
}
}catch (XMLStreamException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
publicvoid test04() {
XMLInputFactoryfactory = XMLInputFactory.newInstance();
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//基于迭代模型的操作方式
XMLEventReaderreader = factory.createXMLEventReader(is);
intnum = 0;
while(reader.hasNext()){
//通过XMLEvent来获取是否是某种节点类型
XMLEvent event =reader.nextEvent();
if(event.isStartElement()){
//通过event.asxxx转换节点
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");
}
}
num++;
}
System.out.println(num);
}catch (XMLStreamException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
publicvoid test05() {
XMLInputFactoryfactory = XMLInputFactory.newInstance();
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//基于Filter的过滤方式,可以有效的过滤掉不用进行操作的节点,效率会高一些
XMLEventReaderreader = factory.createFilteredReader(factory.createXMLEventReader(is),
newEventFilter() {
@Override
publicboolean accept(XMLEvent event) {
//返回true表示会显示,返回false表示不显示
if(event.isStartElement()){
Stringname = event.asStartElement().getName().toString();
if(name.equals("title")||name.equals("price"))
returntrue;
}
returnfalse;
}
});
intnum = 0;
while(reader.hasNext()){
//通过XMLEvent来获取是否是某种节点类型
XMLEventevent = reader.nextEvent();
if(event.isStartElement()){
//通过event.asxxx转换节点
Stringname = event.asStartElement().getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
num++;
}
System.out.println(num);
}catch (XMLStreamException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
基于XPath的方式就必须把整个文档镀金内存,这样才能整个搜寻。这种方式适合根据条件或者说路径找到特定的节点。
@Test
publicvoid test06() {
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//创建文档处理对象
DocumentBuilderdb = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通过DocumentBuilder创建doc的文档对象
Documentdoc = db.parse(is);
//创建XPath
XPathxpath = XPathFactory.newInstance().newXPath();
//第一个参数就是xpath,第二参数就是文档
NodeListlist = (NodeList)xpath.evaluate("//book[@category='WEB']",doc,XPathConstants.NODESET);
for(inti=0;i<list.getLength();i++) {
//遍历输出相应的结果
Elemente = (Element)list.item(i);
System.out.println(e.getElementsByTagName("title").item(0).getTextContent());
}
}catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (XPathExpressionException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
生成XML
@Test
publicvoid test07() {
try{
XMLStreamWriter xsw =XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
xsw.writeStartDocument("UTF-8","1.0");
xsw.writeEndDocument();
Stringns = "http://11:dd";
xsw.writeStartElement("nsadfsadf","person",ns);//前缀、名称、命名空间
xsw.writeStartElement(ns,"id");
xsw.writeCharacters("1");
xsw.writeEndElement();
xsw.writeEndElement();
xsw.flush();
xsw.close();
}catch (XMLStreamException e) {
e.printStackTrace();
}catch (FactoryConfigurationError e) {
e.printStackTrace();
}
}
@Test
publicvoid test08() {
InputStreamis = null;
try{
is= TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//创建文档处理对象
DocumentBuilderdb = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通过DocumentBuilder创建doc的文档对象
Documentdoc = db.parse(is);
//创建XPath
XPathxpath = XPathFactory.newInstance().newXPath();
Transformer tran =TransformerFactory.newInstance().newTransformer();
tran.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
tran.setOutputProperty(OutputKeys.INDENT,"yes");
//第一个参数就是xpath,第二参数就是文档
NodeListlist = (NodeList)xpath.evaluate("//book[title='LearningXML']", doc,XPathConstants.NODESET);
//获取price节点
Elementbe = (Element)list.item(0);
Elemente = (Element)(be.getElementsByTagName("price").item(0));
e.setTextContent("333.9");
Resultresult = new StreamResult(System.out);
//通过tranformer修改节点
tran.transform(newDOMSource(doc), result);
}catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (XPathExpressionException e) {
e.printStackTrace();
}catch (TransformerConfigurationException e) {
e.printStackTrace();
}catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}catch (TransformerException e) {
e.printStackTrace();
}finally {
try{
if(is!=null)is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}