[jdom-interest] Default namespaces
cowtowncoder at yahoo.com
Tue Dec 16 21:52:43 PST 2008
--- On Tue, 12/16/08, Brian Barnett <brian at calcxml.com> wrote:
> I am not understanding how to use default namespaces with JDOM. Seeing
> strange behavior. Here is a sample XML:
I think it would be good to read a bit about xml namespaces -- this just how namespaces work. Basically, when accessing attributes or elements, you must specify namespace as well as local name, unless element/attribute does not belong to any namespace.
(I suspect this would also be found from JDOM FAQ)
It's just that attributes never use the default namespace, so attributes without prefix are never any namespace (i.e. attributes can only use namespaces with explicit prefix); whereas unprefixed elements are in the default namespace, if one has been bound.
So in this case:
> Now, after loading this document, I can read the attributes of the
> <litleResponse> element just fine using
> rootResponseElem.getAttributeValue(). However, if I try to get the
> <batchResponse> element using the code below, null is
> Element elem =
You are basically asking for element in namespace "", but your batchResponse is in the namespace that 'xmlns="..."' declared ("http://www.litle.com/schema").
So, you need to do
Namespace ns = Namespace.get("http://www.litle.com/schema");
Element elem = rootResponseElem.getChild("batchResponse", ns);
instead (default namespace is lexically scoped within element it is declared in and it descendants -- unless re-defined by a child element).
Hope this helps,
-+ Tatu +-
More information about the jdom-interest