[jdom-interest] Accessor methods and namespaces

keallen at mail.stic.net keallen at mail.stic.net
Sun Aug 11 15:17:11 PDT 2002

   Greetings to everyone...  I'll open by admitting I'm fairly new to XML
and JDOM, so please, no burning pitch ;)

   After some initial use I'm very impressed with JDOM - it took me 4 times
the code to do something with DOM that it did with JDOM, and did it in a
manner that won't have my poor successor hunting me whenever he has to
maintain the code.  I have run into a few basic questions, though.

   The biggest one concerns the use of namespaces in the various accessor
methods of element.  My document uses a default namespace, and only one, so
all elements are part of that namespace.  Since getChild(String) interprets
the lack of a namespace as being NO namespace, I have to specify the
namespace whenever I get a child.  This seemed very cumbersome - I ducked
it by creating a Namespace with the document's default namespace that I use
in all my "gets".  It works, but seems like it would fall apart quickly if
my document had multiple namespaces.

   I did some hunting through the list archives, and understand what is
happening, but not necessarily WHY.  Wouldn't it be easier to have the
single-parameter getChild assume you're looking for it in the same
namespace as the parent element?  Or at the very least, add a new method to
do so.

   My confusion only deepened when I added an attribute get - when I tried
to use my same explicit Namespace trick I used for the getChild, all I got
was null...  Until I took the Namespace out, and just used
getAttribute(String) - that worked fine.  Suddely all my understanding went
right out the window :)

   So, are namespaces handled differently for attributes and elements, as
far as default/none?  Even if so, unless I'm really missing something, the
attribute's namespace should have the same namespace as the element, so the
getAttribute(String, Namespace) should have it it, even if it was redundant.

   All help appreciated in advance.

More information about the jdom-interest mailing list