General

Requirements / dependencies

StaxMate needs following to work:

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


CategoryStaxMate

StaxMateFAQ (last edited 2011-01-05 22:30:55 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC