Java Stax如何实现解析XML

发布时间:2020-10-30 23:20:11 作者:Leah
来源:亿速云 阅读:164

这篇文章运用简单易懂的例子给大家介绍Java Stax如何实现解析XML,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

package org.itat.stax;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.EventFilter;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestStax {

  @Test
  public void test01() {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      XMLStreamReader reader = factory.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());
        }
      }
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } finally {
      try {
        if(is!=null) is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  
  @Test
  public void test02() {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      XMLStreamReader reader = factory.createXMLStreamReader(is);
      while(reader.hasNext()) {
        int type = 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
  public void test03() {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      XMLStreamReader reader = factory.createXMLStreamReader(is);
      while(reader.hasNext()) {
        int type = 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
  public void test04() {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      //基于迭代模型的操作方式
      XMLEventReader reader = factory.createXMLEventReader(is);
      int num = 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
  public void test05() {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      //基于Filter的过滤方式,可以有效的过滤掉不用进行操作的节点,效率会高一些
      XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),
          new EventFilter() {
            @Override
            public boolean accept(XMLEvent event) {
              //返回true表示会显示,返回false表示不显示
              if(event.isStartElement()) {
                String name = event.asStartElement().getName().toString();
                if(name.equals("title")||name.equals("price"))
                  return true;
              }
              return false;
            }
          });
      int num = 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
  public void test06() {
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      //创建文档处理对象
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      //通过DocumentBuilder创建doc的文档对象
      Document doc = db.parse(is);
      //创建XPath
      XPath xpath = XPathFactory.newInstance().newXPath();
      //第一个参数就是xpath,第二参数就是文档
      NodeList list = (NodeList)xpath.evaluate("//book[@category='WEB']", doc,XPathConstants.NODESET);
      for(int i=0;i<list.getLength();i++) {
        //遍历输出相应的结果
        Element e = (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();
      }
    }
  }
  
  @Test
  public void test07() {
    try {
      XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
      xsw.writeStartDocument("UTF-8","1.0");
      xsw.writeEndDocument();
      String ns = "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
  public void test08() {
    InputStream is = null;
    try {
      is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
      //创建文档处理对象
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      //通过DocumentBuilder创建doc的文档对象
      Document doc = db.parse(is);
      //创建XPath
      XPath xpath = XPathFactory.newInstance().newXPath();
      Transformer tran = TransformerFactory.newInstance().newTransformer();
      tran.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
      tran.setOutputProperty(OutputKeys.INDENT, "yes");
      //第一个参数就是xpath,第二参数就是文档
      NodeList list = (NodeList)xpath.evaluate("//book[title='Learning XML']", doc,XPathConstants.NODESET);
      //获取price节点
      Element be = (Element)list.item(0);
      Element e = (Element)(be.getElementsByTagName("price").item(0));
      e.setTextContent("333.9");
      Result result = new StreamResult(System.out);
      //通过tranformer修改节点
      tran.transform(new DOMSource(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();
      }
    }
  }
}
<&#63;xml version="1.0" encoding="UTF-8"&#63;>

<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>

关于Java Stax如何实现解析XML就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Java中解析XML的方式是什么
  2. 怎么实现java DOMParser 解析xml

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java stax 解析xml

上一篇:PHP中bcscale timezone charset的设定案例

下一篇:Java实现md5和base64加密解密操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》