# Libxml and CDATA

**URL:** <https://www.ruby-forum.com/t/libxml-and-cdata/79199>\
**Category:** Ruby\
**Created:** [December 13, 2006, 9:27am UTC](https://www.ruby-forum.com/t/libxml-and-cdata/79199 "2006-12-13T09:27:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [December 13, 2006, 9:27am UTC](https://www.ruby-forum.com/t/libxml-and-cdata/79199/1 "2006-12-13T09:27:46Z")

</div>

Hi,

I need to add a CDATA element to an XML document.

The libxml API documentation says that I can use Node.new\_cdata, but  
this doesn’t work here for some reason (Ubuntu with libxml-ruby  
0.3.4-4): “undefined method ‘new\_cdata’ for XML::Node::Class”

How can I create a CDATA element using libxml?

I was looking at REXML but didn’t like the API at all and would very  
much prefer using libxml.

Regards,  
Thomas

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [December 13, 2006, 11:28am UTC](https://www.ruby-forum.com/t/libxml-and-cdata/79199/2 "2006-12-13T11:28:00Z")

</div>

On Wed, 13 Dec 2006 08:27:19 -0000, Thomas M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I need to add a CDATA element to an XML document.
> 
> The libxml API documentation says that I can use Node.new\_cdata, but  
> this doesn’t work here for some reason (Ubuntu with libxml-ruby  
> 0.3.4-4): “undefined method ‘new\_cdata’ for XML::Node::Class”

Wow, that’s a fairly old version you have there 🙂

> How can I create a CDATA element using libxml?
> 
> I was looking at REXML but didn’t like the API at all and would very  
> much prefer using libxml.

You just need to upgrade your libxml-ruby installation (or pester ubuntu  
to upgrade theirs). new\_cdata was added in the 0.3.8 branch:

require\_gem ‘libxml-ruby’, ‘\>= 0.3.8’

# =\> true

require ‘xml/libxml’

# =\> true

XML::Node.new\_cdata(‘cdata foo’)

# =\>
