Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

5 Minute Tutorial for StaxMate

As per introduction, StaxMate is designed to allow

  • Reading XML content efficiently, correctly and conveniently
  • Writing XML content efficiently, correctly and conveniently

To showcase how this is done, let's have a look at sample use cases.

Writing XML content

Let's start with one of simplest and most common use cases: that of writing (aka generating) XML content. Content can come from variety of sources; here we consider the case where we just want to write explicit content (another common case would be to write content read earlier, but let's reserve that for an advanced example).

So, here's xml document that we want to output (note: indentation is added for readability)

Let's first look at the whole code, and then discuss what it actually does:

The first thing we need to do is to create a StaxMate output factory: here we use automatic introspection that Stax XMLOutputFactory offers (to find any plugged implementation); but more often we can directly instantiate specific class (such as com.ctc.wstx.stax.WstxOutputFactory for Woodstox).
This output factory is full thread-safe (after configuration), and should be reused: usually a single(ton) instance is enough for the whole application or service.

Step 2 is to create the document output object. Document object just denotes document itself, not a root element: but we will add the root element under it (could also add comments, processing instructions).
For demonstration purposes, we choose to output document to a File: we could use a StringWriter, or servlet's OutputStream as well.


Reading XML content

The first step on input side is similar to output side: that of creating the necessary StaxMate factory:


  • No labels