package com.yinhai.liyang.stax; import java.io.IOException; import java.io.InputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.junit.Test; /*** * 基于光标的模型 * */ public class TestStax { /** * 心得:可以通过不同的值得到不同的节点,然后节点怎么操作随你 * */ @Test public void TestStax() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml"); XMLStreamReader reader = factory.createXMLStreamReader(in); while (reader.hasNext()) { int note = reader.next(); if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找 System.out.println(reader.getName()); } else if (note == 4) {// 4代表文本节点 System.out.println(reader.getText().trim()); } else if (note == 2) {// 2代表结束节点 System.out.println("/" + reader.getName()); } } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 心得:可以通过开始节点得到文本属性 * */ @Test public void TestStax01() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml"); XMLStreamReader reader = factory.createXMLStreamReader(in); while (reader.hasNext()) { int note = reader.next(); if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找 String name = reader.getName().toString(); if (name.equals("book")) { System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0)); } } } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 心得:可以通过开始节点得到文本值 * */ @Test public void TestStax02() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml");
// in = new ByteArrayInputStream(xml.getBytes());//当xml是一个字符串的时候,将字符串装换成流 XMLStreamReader reader = factory.createXMLStreamReader(in); while (reader.hasNext()) { int note = reader.next(); if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找 String name = reader.getName().toString(); if (name.equals("title")) { System.out.println(name+":"+reader.getElementText()); }else if(name.equals("price")){ System.out.println(name+":"+reader.getElementText()); } } } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package com.yinhai.liyang.stax; import java.io.IOException; import java.io.InputStream; import javax.xml.stream.EventFilter; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; import org.junit.Test; /*** * 基于迭代的模型 * */ public class TestStax02 { @Test public void TestStax() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml"); XMLEventReader reader = factory.createXMLEventReader(in); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) System.out.println(event.asStartElement().getName() .toString()); } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void TestStaxfilter() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml"); XMLEventReader reader = factory.createFilteredReader( factory.createXMLEventReader(in), new EventFilter() { @Override public boolean accept(XMLEvent event) { if (event.isStartElement()) { //过滤,只遍历开始节点 return true; } return false; } }); int num=0; while (reader.hasNext()) { num++; XMLEvent event = reader.nextEvent(); if (event.isStartElement()) System.out.println(event.asStartElement().getName() .toString()); } System.out.println(num); } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
基于迭代模型的要获得文本值(重点是先要获得名字,才能获得文本值)为毛要先判断开始节点
public void TestStax() { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream in = null; try { in = TestStax.class.getClassLoader().getSystemResourceAsStream( "books.xml"); XMLEventReader reader = factory.createXMLEventReader(in); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if(event.isStartElement()) { String name = event.asStartElement().getName().toString(); if(name.equals("title")) { //必须先获得名字才获得文本值,有了这个语句才能获得文本值 System.out.print(reader.getElementText()+":"); } if(name.equals("price")) { //必须先获得名字才能获得文本值,有了这个语句才能获得文本值 System.out.print(reader.getElementText()+" "); } } } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }