This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes looking for a creative way to get a string from file.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "looking for a creative way to get a string from file.xml " Watch "looking for a creative way to get a string from file.xml " New topic
Author

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

Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
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)


Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

I just see a broken image icon, no image...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Adrian Burlington
Ranch Hand

Joined: Jun 16, 2009
Posts: 75
Hi Rob,

THANKS!!!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looking for a creative way to get a string from file.xml
 
Similar Threads
Try this Applet
how to replace integer in agiven string
WA #1.....word association
end of month calculation
Any one can help!