• 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

How to retrieve CDATA Section

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to retrieve data from a CDATA section out of an xml-file that looks like this:
<?xml version="1.0"?>
<result><![CDATA[
<?xml version="1.0"?>
<library>
<fieldFlagInfo>bla</fieldFlagInfo>
<fieldFlagInfo>blu</fieldFlagInfo>
</library>
]]></result>
is there a way to do this?
Any help is appreciated.
thanx
Alex
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XML in a Nutshell #19 says -

CDATA sections are convenient for human authors, not programs. Parsers are not required to tell you whether a particular block of text came from a CDATA section, from normal character data, or from character data that contained entity references. By the time you get access to the data, these differences may have been washed away.


Cheers,
Dan
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are working with a JAXP compatible parser as a DOM, the "result" Element will have a child node that is of the type CDATAsection. You get the contained text with getValue();
Bill
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William!
Here is code which demonstrates that indeed it works.
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can as well apply the following stylesheet -
=================================================
<xslt:stylesheet
xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslt utput omit-xml-declaration="yes"/>
<xslt:template match="result">
<xslt:value-of disable-output-escaping="yes" select="."/>
</xslt:template>
</xslt:stylesheet>
=================================================
The output would look like this -

<?xml version="1.0"?>
<library>
<fieldFlagInfo>bla</fieldFlagInfo>
<fieldFlagInfo>blu</fieldFlagInfo>
</library>
which is the content of the CDATA section.
=================================================
 
Alex Tosh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To all of you: Thank you very much, i got it working
cheers
Alex
 
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic