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
5
votes
2 answers

Splitting huge xml file >10GB into small chunks using Stax Parser

We have Scenario where we need to split large xml file of size more than 10GB in small chunks. Each chunk should contain 100 or 200 element. Example xml 29 Pankaj
Naveen
  • 366
  • 3
  • 12
5
votes
2 answers

XMLEventWriter from scratch: how do I emit xmlns attribute?

I am trying to write an XML document from scratch using the XMLEventWriter from the StAX API. I cannot figure out how to get the default namespace attribute to be emitted. For example, the default namespace URI string is…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
5
votes
3 answers

StAX parser determination at runtime

I have the following code: XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(inStream); this.encoding = xmlStreamReader.getEncoding(); ... This code runs fine in both…
Lluis Martinez
  • 1,963
  • 8
  • 28
  • 42
5
votes
1 answer

Generate a Stax Writer API for a given XML Schema

I have an XML Schema. I have written a thin layer over Stax to allow producing conforming documents 'on the fly' (I don't want a dom-like API, I need low/no footprint). The API has just methods like: writeCar(String manufacturer) writeWheels(String…
zedoo
  • 10,562
  • 12
  • 44
  • 55
5
votes
3 answers

How to write unescaped XML to XMLStreamWriter?

I have a number of small XML chunks, that should be embedded in one big XML as child elements. Is there any way to write these chunks to XMLStreamWriter without escaping them?
Andrei Petrenko
  • 3,922
  • 3
  • 31
  • 53
5
votes
4 answers

Is there a Java XML API that can parse a document without resolving character entities?

I have program that needs to parse XML that contains character entities. The program itself doesn't need to have them resolved, and the list of them is large and will change, so I want to avoid explicit support for these entities if I can. Here's a…
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
5
votes
1 answer

StAX - how to set XMLInputFactory.IS_VALIDATING to true?

this is my first time using StAX for parsing XML documents (still in the learning stage). During the process to parse an XML document using XMLStreamReader and generate a copy of the document using XMLStreamWriter, I encountered the following…
His
  • 5,891
  • 15
  • 61
  • 82
5
votes
1 answer

Stax parsing - parse children nodes depending on selected parent

I need to parse a huge xml file on server and send it to client. I want to do the parsing on demand - meaning, to only parse and show the parent nodes at first, and when the client clicks on a parent node - to send a request to the server that tells…
user1579191
  • 91
  • 2
  • 10
5
votes
2 answers

End element isn't detected by Stax

I'm reading a XML file same as below: And I expected the below code prints out three e on screen: XMLInputFactory factory =…
masoud
  • 55,379
  • 16
  • 141
  • 208
5
votes
2 answers

Java - XML parser performance : Sun Java Streaming XML Parser (SJSXP) vs Woodstox

I am looking for latest, memory efficient and high-performance java XML parsing API. I need to parse 3 MB to 5 MB XML files. I did google on this and come to know about Sun Java Streaming XML Parser (SJSXP) and Woodstox is much faster than DOM &…
Santosh
  • 782
  • 4
  • 15
  • 38
5
votes
1 answer

self-closing tags with XMLEventWriter

So the question is pretty much as stated in the title. I am doing some xml work and using XMLEventWriter. The big issue I'm having is that I need to create some self closing tags The problem is that I haven't figured out a way to do this with…
user141444
  • 53
  • 1
  • 3
5
votes
1 answer

Why does the STAX parser think this is valid XML 1.0 but not 1.1?

In the following code example, I use the STaX parser to parse a piece of XML. If I run the xml10 through it, it works as expected. The xml11 string (which is the same, except for the xml version) - it throws a NullPointerException. I'm running this…
Kylar
  • 8,876
  • 8
  • 41
  • 75
5
votes
1 answer

StAX and namespaces

I am trying to convert some code from using DOM (via jDOM) to use StAX instead. At the same time I am migrating from DTD-based validation to XSD_based validation. Oh, and just for good measure I am introducing JAXB into the equation :) Anyway, as…
Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46
4
votes
3 answers

Parsing multiple XML fragments with STaX

I was hoping the following would be parseable in StAX, But it chokes when you reach the second element. As there is no common root element. (I'm not too sure why a pull parser cares about this particular…
Iain
  • 1,797
  • 1
  • 20
  • 38
4
votes
1 answer

How to configure the behavior of the XML parser used by JAX-WS

My intent is to prevent the XML parser from referencing external entities, defined in a DOCTYPE section at the beginning of the request's XML, but I'm interested in being able to more generally configure the XML parser used by the JAX-WS…
vagelis
  • 334
  • 3
  • 9