Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDOM returns null exception

 
Veljko Cankovic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have file in format below and want to have string field only with names of cities.

<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><![CDATA[<NewDataSet>
<Table>
<Country>Finland</Country>
<City>Halli</City>
</Table>
<Table>
<Country>Finland</Country>
<City>Vaasa</City>
</Table>
</NewDataSet>]]></GetCitiesByCountryResult>
</GetCitiesByCountryResponse>
</soap:Body>
</soap:Envelope>


I must process it with Java. Everything is ok processed until NewDataSet tag which ruturns null always
erData is StructureData type


String citynames=" ";

Document erJdom = erData.getData();
Element root = erJdom.getRootElement();
Namespace erJdomNamespace = root.getNamespace();

Element item = root.getChild("GetCitiesByCountryResult", erJdomNamespace);
//(item.getText()) after thishere returns correct value everything works fine


//but from this I am getting null value:

Element newData1=item.getChild("NewDataSet",erJdomNamespace); //here is NULL


List tables = item.getChildren("TABLE");



for (int j = 0; j < tables.size(); j++)
{
erData.setAsCurrent(tables, j);



gradovi = gradovi + erData.getCurrentData("CITY");

}


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the text inside the <![CDATA[><NewDataSet> is NOT processed as XML but as a lump of text.

You would have to extract that text and parse it to a new DOM before you can use DOM methods on it.

I had a client that got stuck with a bunch of XML and code in that style - its a real pain to manipulate/edit/replace data inside a CDATA set as XML.

Bill
 
g tsuji
Ranch Hand
Posts: 669
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it is very often and quite natural to deliver payload in a string which actually be the serialized xml document.

You can do it like this on a general plot. Replace this line:
>Element newData1=item.getChild("NewDataSet",erJdomNamespace); //here is NULL
by this block (with catching such as JDOMException on the way):

Then you are probably good to go ahead with it as what you've conceived therefrom.
 
Veljko Cankovic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William and g tsuji, you really helped me.
All the best!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic