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
6
votes
6 answers

How to Merge two XMLs in Java

I am trying to merge two xmls in Java. I am using STaX API to write these XMLs. I searched a lot on internet on how to merge xmls but none seems as straight forward as C#. Is there any straight-forward way of doing this in Java using StAX? Probably…
nilesh
  • 14,131
  • 7
  • 65
  • 79
6
votes
1 answer

How to transform XMLStreamReader to XMLStreamWriter

Should be easy and obvious but I cant find a way - the XMLOutputFactory accepts anly OutputStream, Result or another Writer to generate a new XMLStreamWriter. What I have at hand is an XMLStreamReader which has no methods for extracting a Result or…
kostja
  • 60,521
  • 48
  • 179
  • 224
6
votes
6 answers

stax - get xml node as string

xml looks like so: ...stuff... ...stuff... I'm using stax to process one "" at a time and I got that…
Jason
  • 2,451
  • 2
  • 23
  • 31
6
votes
1 answer

Writing stax XML to String

I am using stax to create XML document that I need for my web app. Currently I am creating my XML in a file like this: XMLOutputFactory factory = XMLOutputFactory.newInstance(); String output=null; try { …
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
6
votes
1 answer

XMLStreamWriter remove prolog

I'm writing an xml using XMLStreamWriter. But I do not need the prolog. How can I omit this line in my output xml.
Sorter
  • 9,704
  • 6
  • 64
  • 74
6
votes
3 answers

Can a part of XML be marshalled using JAXB (or JAXB + StAX)?

I have an XML structure which is very huge. I want to update the parts of this XML, by unmarshalling one element and then applying business logic. I am able to unmarshal a child element into a POJO. I want to make changes to this POJO in Java and…
metal7
  • 265
  • 1
  • 3
  • 9
6
votes
1 answer

XJC doesn't generate the @XmlElement with namespace?

I want to parse data using JAXB for the the following XSD schema http://www.uniprot.org/support/docs/uniprot.xsd . A typical XML for this looks like this: http://www.uniprot.org/uniprot/Q8NEJ9.xml My Classes were generated using: xjc…
Pierre
  • 34,472
  • 31
  • 113
  • 192
6
votes
0 answers

XMLStreamException :Trying to output second root, StaXparser

writer.writeStartElement(startElement.getName().getLocalPart()); //when i run on standalone machine with one thread, it works fine, but when i run the multiple jobs with multiple threads on the server it gives me the following error at …
srp
  • 521
  • 11
  • 22
6
votes
2 answers

How to load a relative system DTD into a StAX parser?

I am using woodstox to implement a StAX parser for XML files. Assume that I have a valid XML file with matching DTD somewhere in a common directory in my filesystem. /path/to/test.xml /path/to/test.dtd The XML references to its DTD using a relative…
MRA
  • 2,992
  • 1
  • 16
  • 18
6
votes
3 answers

Small modification to an XML document using StAX

I'm currently trying to read in an XML file, make some minor changes (alter the value of some attributes), and write it back out again. I have intended to use a StAX parser (javax.xml.stream.XMLStreamReader) to read in each event, see if it was one…
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
5
votes
2 answers

Need a CDATA event notifying stax parser for java

I have taken over the maintenance of an application that uses a stax parser to break down an XML file of many records into individual records for further processing. Using this type of parser for this purpose is overkill in my opinion but I didn't…
user1110046
  • 53
  • 1
  • 4
5
votes
1 answer

SPARQL XML results from DBpedia and Jena

I get the following XML from the DBpedia SPARQL end point:
Mulone
  • 3,603
  • 9
  • 47
  • 69
5
votes
2 answers

Java XML: using DOM with StAX to construct a document

I am using StAX to construct an XML document using an XMLStreamWriter. However, there are portions of my document where it is difficult to make calls to XMLStreamWriter's methods one-by-one, and it would be easier to construct a small document…
Jason S
  • 184,598
  • 164
  • 608
  • 970
5
votes
3 answers

How to override 'org.apache.cxf.stax.maxChildElements' property value inside a TomEE container?

I've got a JAX-WS web service endpoint configured purely via annotations running in TomEE 7 environment. Basically, the method being called has to return a List of all node names contained in a graph data structure. The response of such a…
MWiesner
  • 8,868
  • 11
  • 36
  • 70
5
votes
5 answers

How to remove xmlns attribute from the root element in xml and java

I would like to remove the xmlns attribute from following xml string. I have written a java program but not sure if it does what needs to be done here. How do I remove the xmlns attribute and get the modified xml string ? Input XML string:
Nital
  • 5,784
  • 26
  • 103
  • 195