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
12
votes
3 answers

Unmarshalling XML to three lists of different objects using STAX Parser

Is there a way I can use STAX parser to efficiently parse an XML document with multiple lists of objects of different classes (POJO). The exact structure of my XML is as follows (class names are not real)
Nick Div
  • 5,338
  • 12
  • 65
  • 127
12
votes
8 answers

Convert XML file to CSV in Java

Here is an example XML (case 1) : 4504216603 10:00:10.000Z 10:00:30.000Z
ant
  • 22,634
  • 36
  • 132
  • 182
12
votes
5 answers

Java: IndentingXMLStreamWriter alternative?

I am using StAX to create a quite large xml document. Until now I was using the IndentingXMLStreamwriter class to get a well formatted document (see also this answer). A few days ago we setup a jenkins server with an older jdk version (6.26), on…
Lars
  • 320
  • 1
  • 4
  • 13
11
votes
2 answers

Error while parsing XML file with StAx

I wrote a xml parser with StAx that I use to parse XML streams received from the server.Here is my code : private Map parse(InputStream is) throws XMLStreamException { XMLInputFactory factory =…
Dimitri
  • 8,122
  • 19
  • 71
  • 128
11
votes
4 answers

Read XML String using StAX

I am using stax for the first time to parse an XML String. I have found some examples but can't get my code to work. This is the latest version of my code: public class AddressResponseParser { private static final String STATUS = "status"; …
sdoca
  • 7,832
  • 23
  • 70
  • 127
11
votes
4 answers

why is sax parsing faster than dom parsing ? and how does stax work?

somewhat related to: libxml2 from java yes, this question is rather long-winded - sorry. I kept is as dense as I felt possible. I bolded the questions to make it easier to peek at before reading the whole thing. Why is sax parsing faster than dom…
andersonbd1
  • 5,266
  • 14
  • 44
  • 62
11
votes
2 answers

Why StAX classes were not retrofitted for ARM in Java 7

I expected to find XMLStreamReader to be AutoCloseable in Java 7. However, that is not the case. Is there a technical reason why StAX reader/writer interfaces were not (or should not be) retrofitted to implement AutoCloseable ? They already have…
Deepak
  • 225
  • 1
  • 8
10
votes
5 answers

XMLEventWriter: how can I tell it to write empty elements?

I do not see an option within javax.xml.stream.XMLEventWriter or javax.xml.stream.XMLOutputFactory to set either up in a way so that empty elements are written (instead of explicit start and end element pairs). I see that Woodstox has a property to…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
9
votes
7 answers

How do I write unescaped XML outside of a CDATA

I am trying to write XML data using Stax where the content itself is HTML If I try xtw.writeStartElement("contents"); xtw.writeCharacters("here"); xtw.writeEndElement(); I get this <b>here</b> Then I notice…
kazanaki
  • 7,988
  • 8
  • 52
  • 79
9
votes
2 answers

XMLStreamWriter - Java 8 - writeCharacters -

The behavior of this method has changed in Java 8, it seems. I need some quick-fix for my problem. The problem is what I have some code which writes CR and LF after each XML node named . Now (as we migrated to Java 8), instead of CR and LF the…
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
9
votes
2 answers

Validation using JAXB and Stax to marshal XML document

I have created an XML schema (foo.xsd) and used xjc to create my binding classes for JAXB. Let's say the root element is collection and I am writing N document objects, which are complex types. Because I plan to write out large XML files, I am…
bajafresh4life
  • 12,491
  • 5
  • 37
  • 46
9
votes
3 answers

Can XSLT processors be multi-threaded?

I'm fishing for approaches to a problem with XSLT processing. Is it possible to use parallel processing to speed up an XSLT processor? Or are XSLT processors inherently serial? My hunch is that XML can be partitioned into chunks which could be…
Ben Simmons
  • 1,858
  • 3
  • 21
  • 31
9
votes
3 answers

StAX XML all content between two required tags

Starting learning the StAX, using XMLStreamReader, I faced with some problem. How can I get ALL content between tags as Text? I mean, I know name of needed tag, and when I find it, I must go to the close tag, and everything I found between them I…
Den Doeson
  • 168
  • 1
  • 13
8
votes
1 answer

How to enable non-IANA encodings when using javax.xml.stream.XMLStreamReader

I'm using javax.xml.stream.XMLStreamReader to parse XML documents. Unfortunately, some of the documents I'm parsing use non-IANA encoding names, like "macroman" and "ms-ansi". For example: This…
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
8
votes
4 answers

StAX - Setting the version and encoding using XMLStreamWriter

I am using StAX for creating XML files and then validating the file with and XSD. I am getting an error while creating the XML file: javax.xml.stream.XMLStreamException: Underlying stream encoding 'Cp1252' and input paramter for writeStartDocument()…
Anurag
  • 311
  • 4
  • 8
  • 14
1
2
3
43 44