aspose file tools*
The moose likes XML and Related Technologies and the fly likes String from 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 "String from xml" Watch "String from xml" New topic
Author

String from xml

Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
Hello All!
I have an xml doc which I am parsing using SAX. The xml is like :
<root>
<header>xxxxxxx</header>
<body>
<some-tag>
<some-other-tag attrib1="yyyy">
some data
</some-other-tag>
</some-tag>
</body>
</root>

What I need is the contents of the 'body' tag as a string:

<body>
<some-tag>
<some-other-tag attrib1="yyyy">
some data
</some-other-tag>
</some-tag>
</body>

I have the org.w3c.dom.Element object of body. but what call of Element will give me the String within?
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Originally posted by Sachin Deshpande:
What I need is the contents of the 'body' tag as a string:

<body>
<some-tag>
<some-other-tag attrib1="yyyy">
some data
</some-other-tag>
</some-tag>
</body>

I have the org.w3c.dom.Element object of body. but what call of Element will give me the String within?



By saying 'contents' of the Body tag, you understand that you are asking for the text value. So, if I am not mistaken you want the string 'some data ' to be returned in this case. Is that what you are looking for ?

- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
no. i require everything within the 'body' tag; the tags, the values, the attributes, everything, as a string (basically a substring of the entire xml). How would one go about it?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
There's no easy way of doing that but there might be a way if you're not too picky about character-to-character accuracy.

Since SAX is an event-based API, you should implement your handler using the following logic:

1) when you encounter the start element for <body>, you switch into "echo mode" and only come out of that mode once the SAX parser hands you a matching closing tag, </body>.

2) when inside this "echo mode", your SAX handler should process the events as follows:
2.1) startElement(...) should reconstruct the starting tag from the given parameters and append that to a StringBuffer somewhere
2.2) characters(...) should append the given characters as-is into the StringBuffer
2.3) endElement(...) should reconstruct the ending tag and append that to the StringBuffer
2.4) ...and so forth for the remaining methods of the SAX handler interface


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
pranav pranav
Greenhorn

Joined: Mar 28, 2005
Posts: 9
Hi
Did you get the answer?
I am looking for the same thing...
Amit G Piplani
Ranch Hand

Joined: Jul 21, 2004
Posts: 83
Element.getFirstChild().getNodeValue() will give you the element's value


SCJP,SCWCD,SCBCD,SCDJWS1.4, SCDJWS1.5
RAN BAS
Greenhorn

Joined: Mar 28, 2005
Posts: 6
There is a util class in Axis package which provides way to convert a element to string.
org.apache.axis.utils.XMLUtils.ElementToString((Element)doc.getElementsByTagName("body").item(0));
Otherwise you have to achieve it manually by either your own logic in SAX parser or DOM parser.

regards,
Ranjan
 
Don't get me started about those stupid light bulbs.
 
subject: String from xml