This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting elements from JDOM -two namespaces in XML?

 
Veljko Cankovic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20954
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20954
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic