Meaningless Drivel is fun!
The moose likes XML and Related Technologies and the fly likes Need Help - XML Parsing - JDOM 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 "Need Help - XML Parsing - JDOM" Watch "Need Help - XML Parsing - JDOM" New topic

Need Help - XML Parsing - JDOM

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
i am using jdom. i tried getText, getChildText etc. Nothing worked. Please help.

<xinfo name="A">a</xinfo>
<xinfo name="B">b</xinfo>
<xinfo name="C">c</xinfo>
<xinfo name="D">d</xinfo>

how can i extract the content of <xinfolist>? so that i can print as shown below.

<xinfo name="A">a</xinfo>
<xinfo name="B">b</xinfo>
<xinfo name="C">c</xinfo>
<xinfo name="D">d</xinfo>


Vinod Borole

Joined: Feb 25, 2009
Posts: 26
I suggest go for XPath
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I doubt that you can get that list of elements in one block of text (unless they're in a CDATA section, but I'm assuming they're not).

XML APIs are meant for working with XML elements, tags and attributes - if you want to get a string that's made up of several tags, then you'll need to assemble that yourself by traversing through the xinfolist child elements.
Sanjay Virkar

Joined: Apr 03, 2005
Posts: 19
Your Jdom library has a class XMLOutputter.

1) Define a variable of type XMLOutputtter

XMLOutputter strOutputter = new XMLOutputter();

2) Define a string variable.

String l_strXmlForElement;

3) Once you read your element using jdom, (which may or may not have childrens i.e. complex or simple type)
use xmloutputter's outputString method to capture xml for that element in String variable

l_strXmlForElement = strOutputter_.outputString(l_cElem);

This string will contain entire XML element in it. You can print this string.

One suggestion.
Working in Jdom is simple but if your xsd changes frequently or if it is complex, You have to write too much of code to read each element, validate it,
fetch sub elements, traverse list of childrens etc. So consider using xml<>java binding like Castor or Jaxb.

With it you follow simple steps.

1) Defiine schema (.xsd) for for xml
2) Generate code for the schema using readymade code generators.
3) Use classes created in step 2 to marshal, unmarshal, validate, print xml content.

Hope this helps

I agree. Here's the link:
subject: Need Help - XML Parsing - JDOM
It's not a secret anymore!