[XOM-interest] How do you get CDATA types within a text element?

lenards at email.arizona.edu lenards at email.arizona.edu
Wed Aug 6 17:50:04 EDT 2008


Hello XOM-interest,

I need to deliver documents that have CDATA types like the following:

<agent role="photographer">
  <![CDATA[<i>James Pataki</i>]]>
</agent>

I see in the JavaDoc for the Text class the mention that "CDATA sections are not
treated differently than normal text".  And I noticed that there is a CDATA type
defined in child class Attribute.Type.

My code looks like this:

	Element agentEl = new Element("agent", NS);
	agentEl.addAttribute(new Attribute("homepage", homepage));
	agentEl.addAttribute(new Attribute("role",
                             AgentRole.Author.toString()));
	agentEl.appendChild(new Text("<i>" + key + "</i>"));

Now, I understand that I get escaping of characters for free (or at least that's
the behavior I'm seeing).

<agent homepage="http://www.six-fifty.com/"
       role="Author">
&lt;i&gt;James Pataki&lt;/i&gt;</agent>

But I've been asked to put CDATA sections around the inner-text (even though
I've been told that's not really the correct approach anymore, escaping is).

I would expect that there might be a way to control or override the Text class's
behavior.  Or extend it.  Or maybe I can't.  I know that XOM is lightweight, and
well-designed, so maybe this use case isn't supported because it's a bad
approach.

Anyway, I can't find an example nor figure out how to get a CDATA section
emitted inside an element.  Is this even possible with XOM?  Can I reproduce
the first example listed?

Thanks in advance!

Andy







More information about the XOM-interest mailing list