Webservice-XML与Java(五)

处理XML的有很多工具,比如SAX、DOM4J等,还有一种解析XML方式是stax,此为oracle公司提出基于流(stream)来处理的方式,在Java中封装成了stax,和sax很像,在webservice中一般使用基于流的工具,基于dom的或多或少的会影响一些效率。WS中还要涉及到Java对象和XML之间的转换,可以直接使用JDK提供的JAXB。类似的还有:XStream、Jackson、json-lib,这些框架提供了xml和json,json和java对象的转换,根据具体的需求可选择不同的框架。

Jaxb的用法很简单,把Java对象转换为xml叫编排,xml转换为Java对象叫反编排,实例如下:

 

[java][/java] view plaincopy

  1. package com.tgb.xml;
  2. import java.io.StringReader;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import javax.xml.bind.Unmarshaller;
  7. import org.junit.Test;
  8. public class TestJaxb {
  9.     @Test
  10.     public void test01(){
  11.         try {
  12.             JAXBContext ctx = JAXBContext.newInstance(Student.class);
  13.             Marshaller marshaller = ctx.createMarshaller();
  14.             Student stu = new Student(1,”这是”,”32″,new Classroom(1,”计算机”,”2012″));
  15.             marshaller.marshal(stu, System.out);
  16.         } catch (JAXBException e) {
  17.             e.printStackTrace();
  18.         }
  19.     }
  20.     @Test
  21.     public void test02(){
  22.         try {
  23.             String xml = “<?xml version=\”1.0\” encoding=\”UTF-8\” standalone=\”yes\”?><student><age>32</age><classroom><grade>2012</grade><id>1</id><name>计算机</name></classroom><id>1</id><name>这是</name></student>”;
  24.             JAXBContext ctx = JAXBContext.newInstance(Student.class);
  25.             Unmarshaller um = ctx.createUnmarshaller();
  26.             Student stu = (Student)um.unmarshal(new StringReader(xml));
  27.             System.out.println(stu.getName() + “,” + stu.getClassroom().getName());
  28.         } catch (JAXBException e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32. }

 

下面主要讲stax操作xml的实例,首先创建一个xml文档:

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”ISO-8859-1″?>
  2. <bookstore>
  3.     <book category=”COOKING”>
  4.         <title lang=”en”>
  5.             Everyday Italian
  6.         </title>
  7.         <author>Giada De Laurentiis</author>
  8.         <year>2005</year>
  9.         <price>30.00</price>
  10.     </book>
  11.     <book category=”CHILDREN”>
  12.         <title lang=”en”>Harry Potter</title>
  13.         <author>J K. Rowling</author>
  14.         <year>2005</year>
  15.         <price>29.99</price>
  16.     </book>
  17.     <book category=”WEB”>
  18.         <title lang=”en”>XQuery Kick Start</title>
  19.         <author>James McGovern</author>
  20.         <author>Per Bothner</author>
  21.         <author>Kurt Cagle</author>
  22.         <author>James Linn</author>
  23.         <author>Vaidyanathan Nagarajan</author>
  24.         <year>2003</year>
  25.         <price>49.99</price>
  26.     </book>
  27.     <book category=”WEB”>
  28.         <title lang=”en”>Learning XML</title>
  29.         <author>Erik T. Ray</author>
  30.         <year>2003</year>
  31.         <price>39.95</price>
  32.     </book>
  33. </bookstore>

 

基于光标的查找:

 

[java][/java] view plaincopy

  1. @Test
  2. public void test01(){
  3.     XMLInputFactory factory = XMLInputFactory.newInstance();
  4.     InputStream is = null;
  5.     is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  6.     XMLStreamReader reader;
  7.     try {
  8.         reader = factory.createXMLStreamReader(is);
  9.         while(reader.hasNext()){
  10.             int type = reader.next();
  11.             if(type == XMLStreamConstants.START_ELEMENT){ //起始节点
  12.                 System.out.println(reader.getName());
  13.             }else if(type == XMLStreamConstants.CHARACTERS){ //文本节点
  14.                 System.out.println(reader.getText());
  15.             }else if(type == XMLStreamConstants.END_ELEMENT){ //结束节点
  16.                 System.out.println(“/” + reader.getName());
  17.             }
  18.         }
  19.     } catch (XMLStreamException e) {
  20.         e.printStackTrace();
  21.     }finally{
  22.         if(is != null){
  23.             try {
  24.                 is.close();
  25.             } catch (IOException e) {
  26.                 e.printStackTrace();
  27.             }
  28.         }
  29.     }
  30. }
  31. @Test
  32. public void test02(){
  33.     XMLInputFactory factory = XMLInputFactory.newInstance();
  34.     InputStream is = null;
  35.     is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  36.     XMLStreamReader reader;
  37.     try {
  38.         reader = factory.createXMLStreamReader(is);
  39.         while(reader.hasNext()){
  40.             int type = reader.next();
  41.             if(type == XMLStreamConstants.START_ELEMENT){
  42.                 String name = reader.getName().toString();
  43.                 if(“book”.equals(name)){
  44.                     //读取属性名和值
  45.                     System.out.println(reader.getAttributeName(0) + “:” + reader.getAttributeValue(0));
  46.                 }
  47.                 //获取元素内容
  48.                 if(“price”.equals(name)){
  49.                     System.out.println(reader.getElementText() + “\n”);
  50.                 }
  51.             }
  52.         }
  53.     } catch (XMLStreamException e) {
  54.         e.printStackTrace();
  55.     }finally{
  56.         if(is != null){
  57.             try {
  58.                 is.close();
  59.             } catch (IOException e) {
  60.                 e.printStackTrace();
  61.             }
  62.         }
  63.     }
  64. }

 

基于迭代模型查找:

 

[java][/java] view plaincopy

  1. @Test
  2.     public void test03(){
  3.         XMLInputFactory factory = XMLInputFactory.newInstance();
  4.         InputStream is = null;
  5.         is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  6.         try {
  7.             //基于迭代模型的操作方式
  8.             XMLEventReader reader = factory.createXMLEventReader(is);
  9.             int num = 0;
  10.             while(reader.hasNext()){
  11.                 //通过XMLEvent来获取是否是某种节点类型
  12.                 XMLEvent event = reader.nextEvent();
  13.                 if(event.isStartElement()){
  14.                     //通过event.asxxx转换节点
  15.                     String name = event.asStartElement().getName().toString();
  16.                     if(“title”.equals(name)){
  17.                         System.out.println(reader.getElementText() + “:”);
  18.                     }
  19.                     if(“price”.equals(name)){
  20.                         System.out.println(reader.getElementText() + “\n”);
  21.                     }
  22.                 }
  23.                 num++;
  24.             }
  25.             System.out.println(num);
  26.         } catch (XMLStreamException e) {
  27.             e.printStackTrace();
  28.         }finally{
  29.             if(is != null){
  30.                 try {
  31.                     is.close();
  32.                 } catch (IOException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.         }
  37.     }

 

过滤器的使用:

 

[java][/java] view plaincopy

  1. @Test
  2.     public void test04(){
  3.         XMLInputFactory factory = XMLInputFactory.newInstance();
  4.         InputStream is = null;
  5.         is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  6.         try {
  7.             //基于Filter的过滤方式,可以有效的过滤不用进行操作的节点,效率高
  8.             XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is), new EventFilter() {
  9.                 @Override
  10.                 public boolean accept(XMLEvent event) {
  11.                     if(event.isStartElement()){
  12.                         String name = event.asStartElement().getName().toString();
  13.                         if(name.equals(“title”) || name.equals(“price”)){
  14.                             return true;
  15.                         }
  16.                     }
  17.                     return false;
  18.                 }
  19.             });
  20.             int num = 0;
  21.             while(reader.hasNext()){
  22.                 XMLEvent event = reader.nextEvent();
  23.                 if(event.isStartElement()){
  24.                     String name = event.asStartElement().getName().toString();
  25.                     if(“title”.equals(name)){
  26.                         System.out.println(reader.getElementText() + “:”);
  27.                     }
  28.                     if(“price”.equals(name)){
  29.                         System.out.println(reader.getElementText() + “\n”);
  30.                     }
  31.                 }
  32.                 num++;
  33.             }
  34.             System.out.println(num);
  35.         } catch (XMLStreamException e) {
  36.             e.printStackTrace();
  37.         }finally{
  38.             if(is != null){
  39.                 try {
  40.                     is.close();
  41.                 } catch (IOException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         }
  46.     }

 

XPath的使用:

 

[java][/java] view plaincopy

  1. @Test
  2.     public void test05(){
  3.         XMLInputFactory factory = XMLInputFactory.newInstance();
  4.         InputStream is = null;
  5.         try{
  6.             is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  7.             //创建文档处理对象
  8.             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  9.             //通过DocumentBuilder创建doc的文档对象
  10.             Document doc = db.parse(is);
  11.             //创建xpath
  12.             XPath xpath = XPathFactory.newInstance().newXPath();
  13.             //第一个参数就是xpath,第二个参数就是文档
  14.             NodeList list = (NodeList)xpath.evaluate(“//book[@category=’WEB’]”, doc, XPathConstants.NODESET);
  15.             for(int i=0;i<list.getLength();i++){
  16.                 //遍历输出相应的结果
  17.                 Element e = (Element)list.item(i);
  18.                 System.out.println(e.getElementsByTagName(“title”).item(0).getTextContent());
  19.             }
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         }finally{
  23.             if(is != null){
  24.                 try {
  25.                     is.close();
  26.                 } catch (IOException e) {
  27.                     e.printStackTrace();
  28.                 }
  29.             }
  30.         }
  31.     }

 

使用XMLStreamWriter创建xml:

 

[java][/java] view plaincopy

  1. @Test
  2.     public void test06(){
  3.         try {
  4.             XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
  5.             xsw.writeStartDocument(“utf-8”, “1.0”);
  6.             xsw.writeEndDocument();
  7.             String ns = “http://www.tgb.com”;
  8.             xsw.writeStartElement(“prefix”,”person”,ns);
  9.             xsw.writeStartElement(ns,”id”);
  10.             xsw.writeCharacters(“1”);
  11.             xsw.writeEndElement();
  12.             xsw.writeEndElement();
  13.             xsw.flush();
  14.             xsw.close();
  15.         } catch (XMLStreamException e) {
  16.             e.printStackTrace();
  17.         } catch (FactoryConfigurationError e) {
  18.             e.printStackTrace();
  19.         }
  20.     }

 

使用Transformer更新节点信息:

 

[java][/java] view plaincopy

  1. @Test
  2.     public void test07(){
  3.         try {
  4.             InputStream is = TestStax.class.getClassLoader().getResourceAsStream(“books.xml”);
  5.             //创建文档处理对象
  6.             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  7.             //通过DocumentBuilder创建doc文档对象
  8.             Document doc = db.parse(is);
  9.             //创建xpath
  10.             XPath xpath = XPathFactory.newInstance().newXPath();
  11.             Transformer tran = TransformerFactory.newInstance().newTransformer();
  12.             tran.setOutputProperty(OutputKeys.ENCODING, “UTF-8”);
  13.             tran.setOutputProperty(OutputKeys.INDENT, “yes”);
  14.             //第一个参数就是xpath,第二个参数就是文档
  15.             NodeList list = (NodeList)xpath.evaluate(“//book[title=’Learning XML’]”,doc,XPathConstants.NODESET);
  16.             //获取price节点
  17.             Element be = (Element)list.item(0);
  18.             Element e = (Element)(be.getElementsByTagName(“price”).item(0));
  19.             e.setTextContent(“232323”);
  20.             Result result = new StreamResult(System.out);
  21.             //修改源
  22.             tran.transform(new DOMSource(doc), result);
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.     }

 

以上都是stax的简单应用,具体可参考JDK。

标签