abhi
abhi

Reputation: 23

Writing out an element with staxmate api

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?

Upvotes: 2

Views: 233

Answers (1)

StaxMan
StaxMan

Reputation: 116572

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)

Upvotes: 1

Related Questions