• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting HTML in XML using JAVA

 
Ashutosh Arya
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try &lt;html&gt;
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certain characters (such as pointy brackets and ampersands) can't occur within XML directly. You'll have to use a CDATA section for that.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if there is a CDATA Section within the XML you are going to embed?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by that? Can you give an example?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic