• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String from xml

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Did you get the answer?
I am looking for the same thing...
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Element.getFirstChild().getNodeValue() will give you the element's value
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic