Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String from xml

 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pranav pranav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Did you get the answer?
I am looking for the same thing...
 
Amit G Piplani
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Element.getFirstChild().getNodeValue() will give you the element's value
 
RAN BAS
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic