• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting xml using java

 
prash patil
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am writting an xml which I have stored in the Oracle DB in clob field.
When I retrieve the xml from DB as String it comes with chars like < etc.

I want to convert the string contains these chars to an XML tags like <, > etc.
Also The string is continious. I want to formated XML.
I am using XMLStreamWritter for writing xml.

Suggestion/Pointers required.

Thanks,
PP
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi prash,

the commons lang project contains an utility class named with which you can convert the XML entities in both directions ;)

Marco
 
prash patil
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Macro!!

Is there any way to format string containing xml tags.
i.e. <entries><entry><templateID>F04FD51B-C537-4d02-BB1D-E89971D1D65</templateID><entryID>41</entryID><subject>Updated Terms and Conditions</subject>

then outpur should be
<entries>
<entry>
<templateID>F04FD51B-C537-4d02-BB1D-E89971D1D65</templateID>
<entryID>41</entryID>
<subject>Updated Terms and Conditions</subject>
etc...
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your sample XML content a simple solution could be to replace the string "><" with ">\n<" to insert a line break between every two XML tags. Of course this is a very simplified idea which won't indent the output and works only for XML similar to your example.

Another possibility would be to really parse the XML content and re-create a new XML output file/stream with known XML APIs like SAX or DOM which can be configured to do pretty printing.

Marco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic