JDOM2 is relatively new (2012) and it is replacing ancient, pre Java 5 generic, JDOM1 version. You can find nice set of basic examples here.
My requirement was to generate xml with single namespace without prefix. Quite common task you can say, but this happend to be little bit tricky. I thought that namespace, once set on Element, should be inherited by child Elements. Of course unless you intentionaly set another namespace on some child. I was wrong!
You must explicitly set very same namespace over and over again on every Element you create. Very annoying indeed. There is a page dedicated to namespace scoping but call me stupid, it haven't help me single bit.
Luckily, after you are finished with creating elements, you can "namespacize" them in one shot with following simple recursive method...
import org.jdom2.Element; import org.jdom2.Namespace; public static void setNamespace(Element element, Namespace namespace) { element.setNamespace(namespace); ListHappy new namespaces!children = element.getChildren(); for (Element child : children) { setNamespace(child, namespace); } }
No comments:
Post a Comment