首页 > 代码库 > 【WebService】Stax的基本操作基于游标

【WebService】Stax的基本操作基于游标

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bookstore>
    <book category="COOKING">
        <title lang="en">Giada De Laurentiis</title>
        <author>Lee</author>
        <year>2005</year>
        <price>30.00</price>
    </book>

    <book category="WEB">
        <title lang="en">JQuery</title>
        <author>Lee</author>
        <author>Sun</author>
        <author>James</author>
        <year>2015</year>
        <price>30.00</price>
    </book>
</bookstore>
  1 package com.slp.stax;
  2 
  3 import org.junit.Test;
  4 
  5 import javax.xml.stream.*;
  6 import javax.xml.stream.events.XMLEvent;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 
 10 /**
 11  * Created by sanglp on 2017/2/26.
 12  */
 13 public class TestStax {
 14 
 15     /**
 16      * Bookstore
 17 
 18      book
 19 
 20      title
 21      Giada De Laurentiis
 22      /title
 23 
 24      author
 25      Lee
 26      /author
 27 
 28      year
 29      2005
 30      /year
 31 
 32      price
 33      30.00
 34      /price
 35 
 36      /book
 37 
 38      book
 39 
 40      title
 41      JQuery
 42      /title
 43 
 44      author
 45      Lee
 46      /author
 47 
 48      author
 49      Sun
 50      /author
 51 
 52      author
 53      James
 54      /author
 55 
 56      year
 57      2015
 58      /year
 59 
 60      price
 61      30.00
 62      /price
 63 
 64      /book
 65 
 66      /bookstore
 67 
 68      */
 69     @Test
 70     public void test01(){
 71         XMLInputFactory factory = XMLInputFactory.newInstance();
 72         InputStream is = null;
 73         try {
 74         is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
 75             XMLStreamReader reader = factory.createXMLStreamReader(is);
 76             while (reader.hasNext()){
 77                 int type = reader.next();
 78                 //判断节点类型
 79                 if(type== XMLStreamConstants.START_ELEMENT){
 80                     System.out.println(reader.getName());
 81                 }else if(type==XMLStreamConstants.CHARACTERS){
 82                     System.out.println(reader.getText().trim());
 83                 }else if(type==XMLStreamConstants.END_ELEMENT){
 84                     System.out.println("/"+reader.getName());
 85                 }
 86                // System.out.println(reader.next());
 87             }
 88         } catch (XMLStreamException e) {
 89             e.printStackTrace();
 90         }finally {
 91             if(is!=null){
 92                 try {
 93                     is.close();
 94                 } catch (IOException e) {
 95                     e.printStackTrace();
 96                 }
 97             }
 98         }
 99     }
100 
101 
102     @Test
103     public void test02(){
104         XMLInputFactory factory = XMLInputFactory.newInstance();
105         InputStream is = null;
106         try {
107             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
108             XMLStreamReader reader = factory.createXMLStreamReader(is);
109             while (reader.hasNext()){
110                 int type = reader.next();
111                 if(type== XMLStreamConstants.START_ELEMENT){
112                     String name = reader.getName().toString();
113                     if(name.equals("book")){
114                     System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
115                     }
116                 }
117             }
118         } catch (XMLStreamException e) {
119             e.printStackTrace();
120         }finally {
121             if(is!=null){
122                 try {
123                     is.close();
124                 } catch (IOException e) {
125                     e.printStackTrace();
126                 }
127             }
128         }
129     }
130 
131 
132     /**
133      * Giada De Laurentiis:30.00
134      JQuery:30.00
135      */
136     @Test
137     public void test03(){
138         XMLInputFactory factory = XMLInputFactory.newInstance();
139         InputStream is = null;
140         try {
141             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
142             XMLStreamReader reader = factory.createXMLStreamReader(is);
143             while (reader.hasNext()){
144                 int type = reader.next();
145                 if(type== XMLStreamConstants.START_ELEMENT){
146                     String name = reader.getName().toString();
147                     if(name.equals("title")){
148                         System.out.print(reader.getElementText()+":");
149                     }
150                     if(name.equals("price")){
151                         System.out.print(reader.getElementText()+"\n");
152                     }
153                 }
154             }
155         } catch (XMLStreamException e) {
156             e.printStackTrace();
157         }finally {
158             if(is!=null){
159                 try {
160                     is.close();
161                 } catch (IOException e) {
162                     e.printStackTrace();
163                 }
164             }
165         }
166     }
167 
168 
169     /**
170      * Giada De Laurentiis:30.00
171      * JQuery:30.00
172      */
173     @Test
174     public void test04(){
175         XMLInputFactory factory = XMLInputFactory.newInstance();
176         InputStream is = null;
177         try {
178             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
179            //基于迭代模型的操作方式
180             XMLEventReader reader = factory.createXMLEventReader(is);
181             while (reader.hasNext()){
182                 //通过XMLEvent来获取是否是某种节点类型
183                 XMLEvent event = reader.nextEvent();
184                 if(event.isStartElement()){
185                     //通过event.asxxx()转换节点
186                     String name = event.asStartElement().getName().toString();
187                     if(name.equals("title")){
188                         System.out.print(reader.getElementText()+":");
189                     }
190                     if(name.equals("price")){
191                         System.out.print(reader.getElementText()+"\n");
192                     }
193                 }
194             }
195         } catch (XMLStreamException e) {
196             e.printStackTrace();
197         }finally {
198             if(is!=null){
199                 try {
200                     is.close();
201                 } catch (IOException e) {
202                     e.printStackTrace();
203                 }
204             }
205         }
206     }
207 
208     /**
209      * Giada De Laurentiis:30.00
210      * JQuery:30.00
211      */
212     @Test
213     public void test05(){
214         XMLInputFactory factory = XMLInputFactory.newInstance();
215         InputStream is = null;
216         try {
217             is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
218             //基于Filter的过滤方式  可以有效的过滤不用进行操作的节点,效率会高一些
219             XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),new EventFilter(){
220                 public boolean accept(XMLEvent event){
221                     //放true表示会显示,返回false表示不显示
222                     if(event.isStartElement())
223                         return true;
224                     return  false;
225                 }
226             });
227             int num =0;
228             while (reader.hasNext()){
229                 //通过XMLEvent来获取是否是某种节点类型
230                 XMLEvent event = reader.nextEvent();
231                 if(event.isStartElement()){
232                     //通过event.asxxx()转换节点
233                     String name = event.asStartElement().getName().toString();
234                     if(name.equals("title")){
235                         System.out.print(reader.getElementText()+":");
236                     }
237                     if(name.equals("price")){
238                         System.out.print(reader.getElementText()+"\n");
239                     }
240                 }
241                 num++;
242             }
243         } catch (XMLStreamException e) {
244             e.printStackTrace();
245         }finally {
246             if(is!=null){
247                 try {
248                     is.close();
249                 } catch (IOException e) {
250                     e.printStackTrace();
251                 }
252             }
253         }
254     }
255 }

 

【WebService】Stax的基本操作基于游标