2

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.

bluish
  • 26,356
  • 27
  • 122
  • 180

3 Answers3

1

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
  • 34,983
  • 11
  • 83
  • 114
1

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
  • 119,590
  • 31
  • 270
  • 376
0

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

Community
  • 1
  • 1
Rodney Gitzel
  • 2,652
  • 16
  • 23