Willem Ghijsen
Willem Ghijsen

Reputation: 21

Display XML in Java

I receive information from a web page as a string of XML, now I want to display that string as I would see it in a web page, i.e. properly formatted. I have tried to do this with JEditorPane, but that only displays HTML. I know this must be possible, since I can display a web page via a link to it.

Upvotes: 2

Views: 2766

Answers (3)

Bruno
Bruno

Reputation: 122749

In addition to @Peter's answer, who suggested to use XSLT, here is an XSLT stylesheet that turns XML documents into HTML content, with pretty colours when combined with its accompanying CSS: xmlverbatim (the documentation explains how to use it, although it assumes you already know about XSLT).

Upvotes: 1

Rodney Gitzel
Rodney Gitzel

Reputation: 2710

I'm not sure what you mean exactly by "as I would see it in a web page" but if you have the ability to display text (or can use the 'pre' tag in JEditorPane), you could "pretty print" the XML.

Here's a JDOM way: http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html

Heck, here's the SO way: How to pretty print XML from Java? :-p

Upvotes: 0

Peter Tillemans
Peter Tillemans

Reputation: 35341

XML is just a way to portably represent (semi-)structured data and in principle the tags have no predefined meaning like HTML tags (with of course the notable exceptions of xhtml and other xml formats which HAVE defined a meaning to the tags).

So in the generic case it is not possible to represent XML in a nicely formatted way.

Typically the XML file is transformed with XSLT or a similar transformation script to turn the XML in a HTML (or similar) representation.

For simple readable representations this is very straightforward. Here is a tutorial.

For specific tips regarding XSLT use in Java see here.

Upvotes: 1

Related Questions