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

Leave entities as-is when parsing XML with Woodstox

I'm using Woodstox to process an XML that contains some entities (most notably >) in the value of one of the nodes. To use an extreme example, it's something like this:   <   > & " '   I have…
luthier
  • 2,674
  • 4
  • 32
  • 35
4
votes
1 answer

how to get the value of attribute processing by STAX using java language?

i want to get the value of attribute of xml file without knowing it's index, since attributes are repeated in more than one element in the xml file. here is my xml file
and here is the…
palAlaa
  • 9,500
  • 33
  • 107
  • 166
4
votes
2 answers

java use StAX to get children elements in a generic fashion

I am trying to use StAX (I already dislike it....) It seems that the only way to use it is by continuous if-else conditions. But most important it seems there is no way to associate an element with its children unless one knows beforehand the…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
4
votes
2 answers

How do I serialize / deserialize a class in XML with Woodstox StAX 2

I'm pretty much trying to archive, what has been done in how-to-serialize-deserialize-simple-classes-to-xml-and-back (C#) in Java. If possible, I would like to avoid writing a serialize / deserialize methods for each class. For example, part of…
Margus
  • 19,694
  • 14
  • 55
  • 103
4
votes
0 answers

formatting xsd validation message in java

when an xml is validated against a XSD schema using javax.xml.validation.Validator class following type of error messages are displayed. cvc-complex-type.2.4.a: Invalid content was found starting with element 'LastName'. One of '{FirstName}' is…
springenthusiast
  • 403
  • 1
  • 8
  • 30
4
votes
2 answers

How to clone an InputStream to allow reading from both streams ( original and clone)?

I have an InputStream of which I read data to log it on the screen. Then, this data I wish to pass it to an StaxParser. However the staxparser doesn't know to feed continuously from string,but rather input stream. So I would like to clone the same…
adragomir
  • 457
  • 4
  • 16
  • 33
4
votes
1 answer

How to read the header of xml

i have xml file, and i want to read the header of it whic is as follows using StAX: in the switch-case statement i ahve the following cases but non of them detects the header switch (event) { case…
rmaik
  • 1,076
  • 3
  • 15
  • 48
4
votes
6 answers

JAVA: gathering byte offsets of xml tags using an XmlStreamReader

Is there a way to accurately gather the byte offsets of xml tags using the XMLStreamReader? I have a large xml file that I require random access to. Rather than writing the whole thing to a database, I would like to run through it once with an…
Dave
  • 1,204
  • 2
  • 14
  • 25
4
votes
2 answers

Modifying one XML file using StAX

I have the following xml file David Berkley 30 25001
DeepN
  • 344
  • 2
  • 13
4
votes
3 answers

Formatting XML file using StAX

I am using StAX XML stream writer to write the XML file. It writes all the data in a single line. I want all the tags to be indented instead of a single line.
Anurag
  • 311
  • 4
  • 8
  • 14
4
votes
0 answers

java StAX - standalone property of StartDocument

I want to read, manipulate and write a xml file. I wanted to start with just read and write and then manipulate later. I use the StAX Parser and want to use EventReader and EventWriter. I encountered my first problem when wanting to read and write…
emi-le
  • 756
  • 9
  • 26
4
votes
2 answers

XML into data structure in java using sax, stax or DOM

So I've been working on this project of mine for the past two weeks and I've not made any headway with this. My issue isn't with parsing the XML file to begin with, but rather what to do with it afterwards. So I've made programs with SAX, StAX and…
user2062207
  • 955
  • 4
  • 18
  • 34
4
votes
2 answers

Alternative to JAXB for XML parsing

I am currently using JAXB to parse XML documents, however i need a better performing XML processor. Better = Faster and decrease memory footprint. I have to process literally millions of separate XML documents. I am using websphere application…
Hector
  • 4,016
  • 21
  • 112
  • 211
4
votes
2 answers

How to satisfy dependencies for Apache POI on Android?

There are many posts about reading .docx files with Apache POI on Android. I write Java program, which do it and want move it Android platform. But XWPFDocument requires xmlbeans.jar, and xmlbeans.jar requires stax-api.jar. And Stax API can not be…
ryabenko-pro
  • 728
  • 1
  • 7
  • 18
4
votes
1 answer

Problems getting XML node text in StAX XMLStreamConstants.CHARACTERS event

While reading an XML file using StAX and XMLStreamReader, I encountered a weird problem. Not sure if its an error or I am doing something wrong. Still learning StAX. So the problem is, In XMLStreamConstants.CHARACTERS event, when I collect node…
Indigo
  • 2,887
  • 11
  • 52
  • 83