1

I am looking for Java Swing component, that will automatically format my messy XML (in one line, see example):

<person><name>Joe</name><surname>Black</surname></person>

etc. etc.

Its not very nice to see like billion lines long line :) And I hope there is some component, that'll do the dirty work for me. Thankx

Edit: I get XML input as plain string from database, so there is no real XML doc for me :(

Xorty
  • 17,616
  • 24
  • 103
  • 151

2 Answers2

5

You don't need to rely on a Swing component to do the formatting. You can try pretty printing your XML and set the formatted output directly in your component. This has been asked before.

Community
  • 1
  • 1
Camilo Díaz Repka
  • 4,807
  • 5
  • 40
  • 68
0

Suggest you use a Java-based DOM. We use XOM (in which case use serializer()) to get the format that you require in a text string, and then repaint your component.

Also see examples in: How to pretty print XML from Java?

Parse the string into the DOM. For example:

Document doc = new Builder().build(new StringReader(yourXML));

to get the outputStream use

os = new ByteArrayOutputStream();

then use the serializer to produce text:

Serializer serializer = new Serializer(os);
serializer.setTheFormatYouWant(... several options in the class ...);
serializer.output(doc);

and then

os.toString();
Community
  • 1
  • 1
peter.murray.rust
  • 36,369
  • 41
  • 146
  • 215