This class requires the StAX interfaces and a StAX parser implementation to be on the classpath. For example Woodstox (recommended) or Sun's sjsxp. Woodstox is the only StAX parser known to be exceptionally conformant, reliable, complete and efficient. At this time, other underlying StAX parsers may not perform full wellformedness checking, tend to have incomplete or buggy support for DTD, entities, external references, and are in general not as mature as underlying SAX parsers such as Xerces.
An instance of this class is not thread-safe.
Example Usage: Print each article in a list of millions of articles via buildFragment()
:
InputStream in = new FileInputStream("samples/data/articles.xml"); XMLStreamReader reader = StaxUtil.createXMLStreamReader(in, null); reader.require(XMLStreamConstants.START_DOCUMENT, null, null); reader.nextTag(); // move to "articles" root element reader.require(XMLStreamConstants.START_ELEMENT, null, "articles"); while (reader.nextTag() == XMLStreamConstants.START_ELEMENT) { // yet another article reader.require(XMLStreamConstants.START_ELEMENT, null, "article"); Document fragment = new StaxParser(reader, new NodeFactory()).buildFragment(); // do something useful with the fragment... System.out.println("fragment = "+ fragment.getRootElement().toXML()); } reader.close(); in.close();Example: Print all events in document order via
buildNode()
: InputStream in = new FileInputStream("samples/data/articles.xml"); XMLStreamReader reader = StaxUtil.createXMLStreamReader(in, null); StaxParser parser = new StaxParser(reader, new NodeFactory()); int depth = 0; int ev; while ((ev = reader.getEventType()) != XMLStreamConstants.END_DOCUMENT) { if (ev == XMLStreamConstants.START_ELEMENT) depth++; // do something useful with the node... Node node = parser.buildNode(); System.out.println(depth + ":" + StaxUtil.toString(ev) + ":" + node.toXML()); if (ev == XMLStreamConstants.END_ELEMENT) depth--; reader.next(); } reader.close(); in.close();Using JDBC 4's SQLXML data type, you could retrieve a user's blog entries from a database as follows:
Connection conn = myDataSource.getConnection(); PreparedStatement st = conn.prepareStatement("select userid, blog_entry from user_has_blog"); ResultSet rs = st.executeQuery(); while (rs.next()) { SQLXML blog = st.getSQLXML("blog_entry"); javax.xml.stream.XMLStreamReader reader = blog.createXMLStreamReader(); Document doc = new StaxParser(reader, new NodeFactory()).build(); System.out.println(doc.toXML()); blog.free(); }@author whoschek.AT.lbl.DOT.gov @author $Author: hoschek $ @version $Revision: 1.20 $, $Date: 2006/06/19 01:38:21 $
|
|
|
|