Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to show an xml content in a Jsp page?  RSS feed

 
Chan Yan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use <jsp:include page=".....xml" /> tag in a jsp file to include an xml file. When this jsp page is open, only the text content in the xml file shows up with plain text. I want this xml file shows up like in IE (with xml format). Does anyone know how to do it? Thanks.
 
Matt W Robinson
Greenhorn
Posts: 12
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you just want the full content to display including the tags. If you want it to 'fold' like the IE display does (by clicking on the '-' signs) you need some serious JavaScript to include that functionality.

The problem you have is the jsp:include tag will insert the xml file as part of the HTML. The browser (which thinks it is rendering a HTML page) will look at the XML tags and ignore them as invalid, resulting in just the data between the tags being displayed as text.

I don't know what you are using behind the scenes, but if you are using struts, I would create an Action that accepts the XML filename as a parameter, and copy the file to the browser translating all '<' characters to the HTML entity '<' and all the '>' characters as '>'. I would then invoke that Action using AJAX so that it behaved like an inline tag such as <img>....
 
Matt W Robinson
Greenhorn
Posts: 12
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - the previous post is confusing because the forum itself rendered the symbols in the text:
... browser translating all '<' characters to the HTML entity '<' and all the '>' characters as '>'. ...
Should read:
... browser translating all '<' characters to the HTML entity '&lt;' and all the '>' characters as '&gt;'

Got caught by my own explanation :-))
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of that is not necessary.

Use <c:import> to load the XML file into a scoped variable, and then use <cut> to display it. The <cut> will automatically convert the markup to HTML entities.
 
Chan Yan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
All of that is not necessary.

Use <c:import> to load the XML file into a scoped variable, and then use <cut> to display it. The <cut> will automatically convert the markup to HTML entities.


Thank you, Matt W Robinson & Bear Bibeault. I tried to use <c:import . It works. The page shows the content of the XML in plain text as a long string. I want to know if any good way to make it looks better, like each item shows on seperated line. Anyway, thank both of you very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you place the output within a <pre> element, it will preserve the formatting.
 
Matt W Robinson
Greenhorn
Posts: 12
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are prepared to go to the lengths I suggested, you can do whatever you like to the XML code, such as colour code it, format it, etc. This is because your code has to do all work (open the file, read the contents, do translations) anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!