File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Getting elements from JDOM -two namespaces in XML? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Getting elements from JDOM -two namespaces in XML?" Watch "Getting elements from JDOM -two namespaces in XML?" New topic
Author

Getting elements from JDOM -two namespaces in XML?

Veljko Cankovic
Greenhorn

Joined: Dec 27, 2011
Posts: 12
I have some external record which is entering in my system so I must take two results which that external record returns(LineID and Kabel). With JDOM I am trying do to it from following XML:



<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getNetworkDataOutput xmlns="http://oss.it.mk/stela/services/tt">
<getNetworkDataResult>
<GetNetworkDataResult xmlns="http://oss.it.mk/stela/datatypes/tt">
<LineID>L0000107</LineID>
<Kabel>40</Kabel>
</GetNetworkDataResult>
</getNetworkDataResult>
</getNetworkDataOutput>
</soap:Body>
</soap:Envelope>


But I am confused with those two bolded namespaces. Can someone tell me if this is correct way to do it?

Document erJdom = erData.getData();
Element root = erJdom.getRootElement();
Namespace n1=Namespace.getNamespace("http://oss.it.mk/stela/services/tt");
Namespace n2=Namespace.getNamespace(""http://oss.it.mk/stela/datatypes/tt");

Element network1 = root.getChild("getNetworkDataResult", n1);
Element network2 = network1.getChild("GetNetworkDataResult", n2);
String lineid=network2.getChildText("LineID",n2);
String kabel=network2.getChildText("Kabel",n2);



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Veljko Cankovic wrote:But I am confused with those two bolded namespaces. Can someone tell me if this is correct way to do it?


So what confuses you? And what does "it" mean in your question?

Perhaps reading a tutorial about XML namespaces would help: XML Namespaces by Example.
Veljko Cankovic
Greenhorn

Joined: Dec 27, 2011
Posts: 12
Thanks for your repy. I am new with JDOM so I wanted to check if this is right way to get values when there are two namespaces. Until now i always worked with Namespace erJdomNamespace = root.getNamespace(); and used everywhere that one namespace like in this example

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
<GetCitiesByCountryResult>
<NewDataSet>
<Table>
<Country>Finland</Country>
<City>Halli</City>
</Table>
..................
But I supose I can not use that way here because there are two namespaces?
So did I write code correctly? I will be able to check it for two days so I want to be prepared in case of some mistakes in code?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Well, you should be able to tell whether you wrote the code correctly, because running it would produce the correct output. Asking people on forums isn't a reliable way to tell whether code is going to run correctly; the only reliable way is to run it and see what happens.

As for the "two namespaces", you seem to have some question about that but it's hard to guess what the question is. Perhaps you could try to ask a question? In the meantime let me try an answer which might be an answer to your question... Neither of those two elements have two namespaces. The default namespace is inherited by child elements, but can also be overridden by child elements. Overriding is what is happening there.

As for your code... I haven't used JDOM myself, but I wouldn't expect a method called "getChild" to get elements which are descendants but not children. (I recommend you review the API documentation for details.) And since the elements you are trying to get are descendants, but not children, of the root element, I wouldn't expect those calls to "getChild" to return those elements.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
>Until now i always worked with Namespace erJdomNamespace = root.getNamespace(); and used everywhere that one namespace like in this example
To work with soap or many other technologies in the industries, it manifests practically all the time within an xml document with multiple namespace. In that aspect, strengthening the understanding of how namespace works is a necessity.

For the case here, LineID and Kabel, both live in the namespace http://oss.it.mk/stela/datatypes/tt inherited from their parent. There would never be any element at the same time living in more than one namespace. The point is to determine which - and it is not a difficult thing to do.

With jdom, you can use its strong implementation on xpath. Suppose the root is already properly established and I start from there.

I think the above give enough coding keywords for you to read about if you want to get more depth into it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting elements from JDOM -two namespaces in XML?
 
Similar Threads
Doubt in soap:header
JDOM returns null exception
<Soap:Body> vs <SOAP-ENV:Body> Parser Error
Response <item xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
How to use org.jdom.NameSpace in setAttribute() method of org.jdom.Element?