XML之StAX(一)
XML之StAX(二)
StAX是绑定在JDK中的,面向流和事件(Stream/Event)的XML解析器。StAX采用的是“pull“方式解析XML,即你的解析方法可以控制什么时候解析器继续向下解析,以及可以在任何位置停止解析。这就像解析方法在从解析器里拉XML事件一样。StAX的这种解析方式,意味着你不得不按照XML文件的元素,逐条移动StAX解析器,这就有点像Iterator或JDBC的ResultSet。StAX有以下特性:
<?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 static void read()throws IOException { XMLInputFactory factory = XMLInputFactory.newFactory(); Reader reader = null; try{ reader = new FileReader("F:\\Codes\\IDEA\\Tools\\data\\xml\\test.xml"); XMLEventReader eventReader = factory.createXMLEventReader(reader); while (eventReader.hasNext()){ XMLEvent event = eventReader.nextEvent(); // streamReader.getEventType() == XMLStreamReader.START_ELEMENT equals streamReader.isStartElement() if(event.isStartElement()){ StartElement startElement = event.asStartElement(); String name = startElement.getName().toString(); if(!name.equals("bookstore") && !name.equals("book")) { System.out.print(name + ": "); System.out.println(eventReader.getElementText()); } else if(name.equals("book")){ System.out.println(startElement.getAttributes().next()); } } } } catch (Exception e){ e.printStackTrace(); } finally { if(reader != null){ reader.close(); } } }
public static void readFilter()throws IOException{ XMLInputFactory factory = XMLInputFactory.newFactory(); InputStream input = null; try{ input = new FileInputStream(new File("F:\\Codes\\IDEA\\Tools\\data\\xml\\test.xml")); XMLEventReader reader = factory.createFilteredReader( factory.createXMLEventReader(input), new EventFilter() { @Override public boolean accept(XMLEvent event) { if(event.isStartElement()){ String name = event.asStartElement().getName().toString(); if(name.equals("title") || name.equals("price")){ return true; } } return false; } }); while (reader.hasNext()){ XMLEvent event = reader.nextEvent(); if(event.isStartElement()){ String name = event.asStartElement().getName().toString(); System.out.println(name + ": " + reader.getElementText()); } } } catch (Exception e){ e.printStackTrace(); } finally { if(input != null){ input.close(); } } }
public static void write(){ XMLOutputFactory factory = XMLOutputFactory.newFactory(); XMLEventFactory eventFactory = XMLEventFactory.newFactory(); try { XMLEventWriter writer = factory.createXMLEventWriter( new FileWriter("F:\\Codes\\IDEA\\Tools\\data\\xml\\iterator.xml")); XMLEvent event = eventFactory.createStartDocument(); writer.add(event); event = eventFactory.createStartElement("test", null, "bookstore"); writer.add(event); event = eventFactory.createStartElement("test", null, "book"); writer.add(event); event = eventFactory.createNamespace("google", "http://www.google.com"); writer.add(event); event = eventFactory.createAttribute("category", "COMPUTER"); writer.add(event); event = eventFactory.createStartElement("test", null, "author"); writer.add(event); event = eventFactory.createCharacters("Giada De Laurentiis"); writer.add(event); event = eventFactory.createEndElement("test", null, "author"); writer.add(event); event = eventFactory.createEndElement("test", null, "book"); writer.add(event); event = eventFactory.createEndElement("test", null, "bookstore"); writer.add(event); writer.flush(); writer.close(); } catch (Exception e){ e.printStackTrace(); } }
<?xml version="1.0" encoding="UTF-8"?> <test:bookstore> <test:book xmlns:google="http://www.google.com" category="COMPUTER"> <test:author>Giada De Laurentiis</test:author> </test:book> </test:bookstore>