Questions tagged [stax]

StAX stands for Streaming API for XML. It's a streaming Java-based, event-driven, pull-parsing API for reading and writing XML documents.

Traditionally, XML APIs are either:

  • tree based (like DOM) - the entire XML content is read and assembled into an in-memory, hierarchical object graph.
  • event based (like SAX) - the application registers to receive events as entities are encountered within the source document.

Both have advantages: tree based parsers allow random access to the document while the latter requires a smaller memory footprint and is usually faster. StAX sits between these two methodologies, because the application "pulls" the data from the XML data stream at its convenience. The application (not the parser) controls how to fetch data from xml. It was introduced in JSR-173 in March 2004 and it's a new feature in JDK 6.0.

StAX implementations have a reader and a writer APIs. Both with two levels: "raw" cursor access and object-based "event" access.

The "raw" cursor access classes included in JDK are:

The "event" based classes included in JDK are:

Other implementations are:

A performance comparison with a few basic sample snippets can be found here.

648 questions
8
votes
1 answer

Java XMLStreamWriter: Outputting Unicode extended characters (non-BMP)

Does anyone know how to correctly output extended characters (non-BMP, more than 1 char) using Java's XMLStreamWriter? For example, trying to output Unicode U+10480 : import java.io.OutputStreamWriter; import…
Adrian Leonhard
  • 7,040
  • 2
  • 24
  • 38
8
votes
1 answer

Is XMLInputFactory thread-safe?

When profiling my application, I noticed that creating instances of XMLInputFactory over and over is very expensive. Is it safe to share its instances across multiple threads? The javadoc doesn't say anything about its thread-safety and searching…
Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54
8
votes
2 answers

Transforming a StAX Source in Java

I have some code like: XMLInputFactory xif = XMLInputFactory.newInstance() TransformerFactory tf = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null) Transformer t = tf.newTransformer() DOMResult result = new…
arlogb
  • 681
  • 1
  • 9
  • 19
8
votes
3 answers

How to modify a huge XML file by StAX?

I have a huge XML (~2GB) and I need to add new Elements and modify the old ones. For example, I have: .... ... .... And want to get: .... …
Eugene
  • 1,037
  • 4
  • 20
  • 34
8
votes
3 answers

StAX parsing from Java NIO channel

I am attempting to receive a stream of XML events over a Java NIO channel. I am new to both NIO and StAX parsing, so I could very easily be overlooking something :) My search has led me to several SAX and StAX implementations, but they all seem to…
Harlan Iverson
  • 168
  • 1
  • 7
7
votes
1 answer

How to add a schema location with StAX

I am using StAX and I want to add a schema location to my xml file. What is the best way to achieve this?
Lars
  • 320
  • 1
  • 4
  • 13
7
votes
2 answers

How to catch Attribute-events with a StAX XML-parser?

I try to parse an XML file with a StAX XML-parser. It give me START_ELEMENT and END_DOCUMENT events but no ATTRIBUTE events. How can I receive ATTRIBUTE events with the StAX parser? My XML:
Jonas
  • 121,568
  • 97
  • 310
  • 388
7
votes
2 answers

Are there any StAX implementation for android?

I want to use StAX API implementation in android 1.6 and above devices. Are there any implementations out there ? I cannot use the jar file directly since it gives issues regarding inner class. If its not available, is there any way I can recompile…
Prabhat
  • 2,261
  • 7
  • 46
  • 74
7
votes
2 answers

How to specify which stax parser to use

I have a woodstox and and java SE 1.6 stax parser in the classpath but woodstox seems to get selected by default. However in certain cases I'd like to use the default Java stax parser. Is there any way to specify which implementation to use?
Fredrik L
  • 1,790
  • 4
  • 21
  • 28
7
votes
1 answer

javax.xml.stream and javax.xml.transform.stream in Java 10

In Java 8, I've been using these packages: import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.stream.StreamSource; Now, in Java 10, they cannot be…
Xdg
  • 1,735
  • 2
  • 27
  • 42
7
votes
2 answers

IndexOutOfBoundsException when processing empty CDATA with Transformer

I want to extract specific nodes from a large XML file. That works well, until a wild CDATA without any content appears. The output: ERROR: '' javax.xml.transform.TransformerException: java.lang.IndexOutOfBoundsException at…
halloei
  • 1,892
  • 1
  • 25
  • 45
7
votes
6 answers

XMLStreamReader and a real stream

Update There is no ready XML parser in Java community which can do NIO and XML parsing. This is the closest I found, and it's incomplete: http://wiki.fasterxml.com/AaltoHome I have the following code: InputStream input = ...; XMLInputFactory…
Yuri Geinish
  • 16,744
  • 6
  • 38
  • 40
7
votes
2 answers

Is there a good XML parser for light-scanning an XML file to get the byte offsets of elements?

We have a system where we're processing XML files where the file itself is too large to fit in memory. As part of processing, we want to quickly scan through to record the offset of relevant elements, so that later on, we can seek immediately to…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
7
votes
3 answers

Eclipse Can't Find Latest Java 1.6 Methods for XMLOutputFactory

I'm trying to use the newFactory() method for XMLOutputFactory, which was added in Java 6. But Eclipse keeps flagging newFactory() as undefined for the type XMLOutputFactory. JRE7 is the only runtime installed and in the build path of new Java…
Shayon Saleh
  • 419
  • 3
  • 7
  • 15
6
votes
5 answers

Convert Java w3c Document to XMLStreamReader

I would like to reuse some existing code in our code base that accepts an XMLStreamReader my application has the required data as a w3c Document. The following example is a minimum test case: public static void main(String[] args) throws Exception…
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
1 2
3
43 44