tags:

views:

45

answers:

3

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.

+1  A: 

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.

Peter Tillemans
A: 

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: http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java :-p

Rodney Gitzel
+1  A: 

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).

Bruno