Default namespaces

View: New views
2 Messages — Rating Filter:   Alert me  

Default namespaces

by ooper01 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Default namespaces

Hi,
I am not understanding how to use default namespaces with JDOM. Seeing strange behavior. Here is a sample XML:

<litleResponse version="4.1" xmlns="http://www.litle.com/schema" response="0" message="Valid Format" litleSessionId="2734282201">

        <batchResponse id="20081216211506" litleBatchId="2734282300" merchantId="044700">
                <authorizationResponse reportGroup="BWB" customerId="88673">
                        <litleTxnId>271178391204</litleTxnId>
                        <orderId>20081216-2115-88673</orderId>
                        <response>000</response>
                        <responseTime>2008-12-17T04:15:06</responseTime>
                        <message>Approved</message>
                        <authCode>11111</authCode>
                        <fraudResult>
                                <avsResult>01</avsResult>
                        </fraudResult>
                </authorizationResponse>
        </batchResponse>
</litleResponse>

And here is some java code that loads it:

SAXBuilder builder = new SAXBuilder();
File inFile = new File("some-file.xml");
Document xmlResponseDoc = builder.build(inFile);
Element rootResponseElem = xmlResponseDoc.getRootElement();

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

by Tatu Saloranta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- 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@...