|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Default namespacesHi,
<litleResponse version="4.1" xmlns="http://www.litle.com/schema" response="0" message="Valid Format" litleSessionId="2734282201"> <batchResponse id="20081216211506" litleBatchId="2734282300" merchantId="044700">
And here is some java code that loads it: SAXBuilder builder = new SAXBuilder();
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 returned: Element elem = rootResponseElem.getChild("batchResponse"); I think it has something to do with the xmlns="http://www.litle.com/schema" attribute because I had trouble with it as part of the request as well. What is the "best practice" for handling default namespaces in JDOM as specified in the above xml example? Thanks, Brian Barnett _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... |
|
|
Re: Default namespaces--- On Tue, 12/16/08, Brian Barnett <brian@...> wrote:
> Hi, > 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 > returned: > > Element elem = > rootResponseElem.getChild("batchResponse"); 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 +- _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... |
| Free embeddable forum powered by Nabble | Forum Help |