General
Requirements / dependencies
StaxMate needs following to work:
- JDK 1.5 or above (due to use of enums, generic types)
A Stax processor (recommended ones are Woodstox and Aalto; one included in JDK 1.6; Sun's SJSXP also works).
Stax2 API -- ideally Stax processor should implement API (Woodstox and Aalto do); if not, a Stax2 wrapper is used to allow StaxMate work using Stax2 API.
Input
Output
Namespaces
How do I add namespace declarations, prefixes?
Namespaces do not need to be explicitly written or declared; StaxMate adds necessary declarations when needed: that is, when an element or attribute is to be written. So, for example:
SMOutputDocument doc = ....; // document to output
SMNamespace ns = doc.getNamespace("http://dot.com", "prefix");
doc.addElement(ns, "rootElement");which would output something like"
<prefix:rootElement xmlns="http://dot.com" />
Note, too, that if you don't care about prefix to use, you can omit second argument to getNamespace and an arbitrary (but guaranteed to be unique) prefix will be used instead.
But if I do want to force namespace declaration?
Sometimes it makes sense to force declaration of the namespace (for example, if all child elements need it, but parent element not; if not forced you may end up with 'xmlns' declaration on every child element, and it would make sense to force declaration in parent element instead). This can be done with:
SMOutputElement root = doc.addElement("root");
elem.predeclareNamespace(root.getNamespace("http://other.com", "pr2"); // will add declaration if it does not yet exist