aspose file tools*
The moose likes XML and Related Technologies and the fly likes Inserting HTML in XML using JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Inserting HTML in XML using JAVA" Watch "Inserting HTML in XML using JAVA" New topic
Author

Inserting HTML in XML using JAVA

Ashutosh Arya
Ranch Hand

Joined: Oct 07, 2008
Posts: 69
Hi,

I am trying to insert HTML code into XML but in the XML it storing as < (for < sign).

I want to insert the '<' character only in XML

Please help me out.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Try &lt;html&gt;
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
There is a possibility of using another method.

You could use javax.xml.transform.Transformer with a DOM object and write your xml in to an attribute. This will escape the < and & etc. so that enclosing XML remains well formed. When parsing back from DOM the escaped XML comes out as original.

I tried this with StAX (cursor API) and works the same way.

Effectively, this works the same way as replacing < with < without having to do it yourself, if you you are already using any of these approaches to write your XML already..

Hope this helps..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Certain characters (such as pointy brackets and ampersands) can't occur within XML directly. You'll have to use a CDATA section for that.


Ping & DNS - updated with new look and Ping home screen widget
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
What if there is a CDATA Section within the XML you are going to embed?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
What do you mean by that? Can you give an example?
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Ulf,
I mean like this..

This is the XML you are working with




This XML you want to include in the original...




As follows...



This is non well formed XML.

The HTML could be XHTML.. which may have CDATA sections.. how can this situation be handled?

Unless of course it is clear that there will be no CDATA sections in the content that will be embedded..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
CDATA sections can't be nested. If the content contains them, then you'll have to escape or encode them somehow before storing them in another CDATA section.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting HTML in XML using JAVA
 
Similar Threads
formatted XML Ouput
A JSP document template for XHTML output
Recreate XML file
how to open xml file into html page
Insert xml file transformed by xsl into JSP