aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need Help - XML Parsing - JDOM" Watch "Need Help - XML Parsing - JDOM" New topic
Author

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.

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


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>

thanks,


$Hi/\bEr
Vinod Borole
Greenhorn

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

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Sanjay Virkar
Greenhorn

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: http://aspose.com/file-tools
 
subject: Need Help - XML Parsing - JDOM
 
Similar Threads
Checking the attribute value before extracting its data
attributes
HTTP Status 405 - HTTP method GET is not supported by this URL
Put a node into a new xml document
Reading a simple XML File