Question

I am using staxmate to write out an xml document. I want to write out xml elements with a prefix/namespace like "pre:elem". I can construct this manually as "pre" + ":" + "elem" and pass it on to the addElement method of staxmate api. But, is there a better way to do it?

Was it helpful?

Solution

Manually constructing is not guaranteed to work, so it is not a good solution (which you probably knew already). The right way is to get a Namespace instance (with suggested prefix) and use that for writing. So, something like:

SMOutputDocument doc = ...;
SMNamespace ns = doc.getNamespace("http://mynamespaces.com", "pre");
SMOutputElement elem = doc.addElement(ns, "root");
// ... and so forth

You can get namespace instances from any container (SMOutputDocument, SMOutputElement).

Without passing namespace object, default is to assume namespace with URI "" (which must be bound to no-prefix).

(note: I'll ask this to be added to StaxMate FAQ)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top