• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

looking for a creative way to get a string from file.xml

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone think on a creative solution for this: I'm trying to output the content of an XML file on the a JTextArea but I wish the format to be neatly like in the pic below (with indentation).

I tried StringTokenizer / matcher pattern but the result are far from desired.

Thanks!

Currently I have the below in one long line (String)


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just see a broken image icon, no image...
 
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's hotlink prevention in action. The image URL is http://postimage.org/image/1gnszfq9w/

The easy way to do this is using a third party library. JDOM has classes XMLOutputter and Format; the latter has static method getPrettyFormat() which returns a Format that adds proper indentation etc. XMLOutputter can output to any Writer, so that includes StringWriter.
A short piece of code:

An alternative is go the hard way:
- maintain the indentation yourself
- when you encounter an element start tag, increase the indentation
- when you encounter an element end tag, decrease the indentation

The latter technique has the advantage that your not limited to a DOM solution, where the entire document is stored in memory. You could also use it for a SAX or StAX solution where you only get the latest element / attributes / text.
 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

THANKS!!!

 
Rob Spoor
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic