Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Let's first look at one possible piece of code to output such a document:

Code Block
borderStylesolid
titleWriteEmployee/StaxMate
borderStylesolid
// 1: need output factory
SMOutputFactory outf = new SMOutputFactory(XMLOutputFactory.newInstance());
SMOutputDocument doc = outf.createOutputDocument(new File("empl.xml"));
// (optional) 3: enable indentation (note spaces after backslash!)
doc.setIndentation("\n  ", 1, 1);
// 4. comment regarding generation time
doc.addComment(" generated: "+new java.util.Date().toString());
SMOutputElement empl = doc.addElement("employee");
empl.addAttribute(/*namespace*/ null, "id", 123);
SMOutputElement name = empl.addElement("name");
name.addElement("first").addCharacters("Tatu");
name.addElement("last").addCharacters("Saloranta");
// 10. close the document to close elements, flush output
doc.closeRoot();

...

Now that we have written some XML content, let's read it back in.
Let's start with the code (that uses xml document that we saw earlier):

Code Block
borderStylesolid
titleReadEmployee.java
borderStylesolid
// 1: need input factory
SMInputFactory inf = new SMInputFactory(XMLInputFactory.newInstance());
// 2: and root cursor that reads XML document from File:
SMHierarchicCursor rootC = inf.rootElementCursor(new File("empl.xml"));
rootC.advance; // note: 2.0 only method; can also call ".getNext()"
int employeeId = rootC.getAttrIntValue(0);
SMHierarchicCursor nameC = rootC.childElementCursor("name").advance();
SMHierarchicCursor leafC = nameC.childElementCursor().advance();
String first = leafC.collectDescendantText(false);
leafC.advance();
String last = leafC.collectDescendantText(false);
rootC.getStreamReader().closeCompletely()

...

Since the original claim was that StaxMate makes things more convenient, let's see what equivalent code for writein would look like, if we didn't have StaxMate:

Code Block
borderStylesolid
titleWriteEmployee/Stax 1.0
borderStylesolid
// 1: need output factory, writer
XMLOutputFactory outf = XMLOutputFactory.newInstance());
FileOutputStream fos = new FileOutputStream(new File("empl.xml"));
XMLStreamWriter xw = outf.createXMLStreamWriter(fos, "UTF-8");
// No way to do automated indentation: must write manually
xw.writeStartDocument(); // not needed with StaxMate
xw.writeComment(" generated: "+new java.util.Date().toString());
xw.writeStartElement("employee");
String idStr = String.valueOf(123);
xw.writeAttribute("id", idStr);
xw.writeCharacters("\n  "); // indent
xw.writeStartElement("name");
xw.writeCharacters("\n    "); // indent
xw.writeStartElement("first");
xw.writeCharacters("Tatu");
xw.writeEndElement(); // for first
xw.writeCharacters("\n    "); // indent
xw.writeStartElement("last");
xw.writeCharacters("Saloranta");
xw.writeEndElement(); // for last
xw.writeCharacters("\n  "); // indent
xw.writeEndElement(); // for name
xw.writeCharacters("\n"); // indent
xw.writeEndElement(); // for employee
xw.writeEndDocument();
xw.close(); // as per Stax 1.0, won't close stream!
fos.close(); // so we need this too

...