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

Formatting XML string?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know a quick way to input a string of XML and format the text?
I've looked at the API (J2SE 6), but nothing looked useful. Seems like this
ought to have been done before...

Really all I'm trying to do is to wrap the lines in a JEditTextArea (a very early version
that doesn't have wrap functionality ). I just thought since it is XML, I could use
a formatter.

Thanks in advance for your help!
 
Sheriff
Posts: 22783
131
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
JDOM has a "pretty" format you can get using Format.getPrettyFormat(). You can then use that in an XMLOutputter instance. The biggest drawback is, however, that it requires you to put the entire XML document into your memory as a org.jdom.Document. Check out JDOM's SAXBuilder for creating a Document from a file or other source.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if you want to stick to the standard Java API, then:
 
Brady Diggs
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both very good suggestions, fellows. Thanks.

Isn't there an easy way to break a String into lines
and then insert each line as a new line in a Document?
I can't append \n to break the lines. That's the rub.
 
Rob Spoor
Sheriff
Posts: 22783
131
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
Transformer and the JDOM formatter have no notion of strings and lines - they work on more abstract notions like documents, elements and attributes.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brady Diggs wrote:Isn't there an easy way to break a String into lines
and then insert each line as a new line in a Document?


It wouldn't take more than about 10 lines of code to do either of the XML-based options already suggested. What's "not easy" about that?
 
reply
    Bookmark Topic Watch Topic
  • New Topic