[jdom-interest] Namespace issues, et al.

Bradley S. Huffman hip at a.cs.okstate.edu
Sun Feb 23 20:36:11 PST 2003


Malachi de AElfweald writes:

> The issue was 2-fold. If I created them with the xmlns in the constructor, 
> I was
> also setting the xsi:schemaLocation attribute on it again -- thus the first 
> problem.
> The second part of it was that I assumed (since textually it is true) that 
> putting
> a tag inside another (like tagA above) inherits the namespace of the parent 
> element.
> This is true in XML, and is true in the textual representation -- but JDOM 
> changes the
> textual representation to break it... Thus, it can not inherit namespaces 
> from the parent
> because of the fact xmlns="" is being added without my consent. If that one 
> piece was not
> added, then it would all inherit correctly.

JDOM does not change anything.  If you run you text through SAXBuilder,
JDOM will build a faithful representation of it with JDOM objects (Element,
Document, DocType, EntityRef, Namespace, and such). It will not change,
add, or delete any information. The problem seems to be you are creating
new objects and adding them to the treewhcih was built from your texual
representation and it's not producing what you think it should.

Let take a simple example, running the following text though SAXBuilder

<root xmlns="my.default.namespace">
  <child1/>
  <child2/>
</root>

Produces the same objects as the following JDOM code

   Namespace defaultNS = Namespace.get("my.default.namespace");
   Element root = new Element("root", defaultNS);
   Text text1 = new Text("\n  ");
   Element child1 = new Element("child1", defaultNS);
   Text text2 = new Text("\n  ");
   Element child2 = new Element("child2", defaultNS);
   Text text3 = new Text("\n");

   root.addContent(text1);
   root.addContent(child1);
   root.addContent(text2);
   root.addContent(child2);
   root.addContent(text3);

   Document document = new Document(root);

Now if you do a

   Element child3 = new Element("child3");

Which is just shorthand for

   Element child3 = new Element("child3", Namespace.NO_NAMESPACE);

It produces a new Element object such that

   child3.getName()            returns "child3"
   child3.getNamespacePrefix() returns ""
   child3.getNamespaceURI()    returns ""

Then adding it to root Element object above

   root.addContent(child3);

Means

   child3.getName()            still returns "child3"
   child3.getNamespacePrefix() still returns ""
   child3.getNamespaceURI()    still returns "", not "my.default.namespace"

And running document through XMLOutputter (without setting newlines
or indent) produces

<root xmlns="my.default.namespace">
  <child1/>
  <child2/>
<child3 xmlns=""/></root>

If you really want the textual representation to be

<root xmlns="my.default.namespace">
  <child1/>
  <child2/>
<child3/></root>

i.e child3 is in the "my.default.namespace" namespace, then it should of
been constructed as follows

   Element child3 = new Element("child3", defaultNS);

Again JDOM does not change anything unless explicitly told to do so.

Brad



More information about the jdom-interest mailing list