• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inserting HTML in XML using JAVA

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try &lt;html&gt;
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if there is a CDATA Section within the XML you are going to embed?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic