Is there Swing component that will automatically format XML?
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'l开发者_开发问答l 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 :(
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.
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();
精彩评论