Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing a CDATA block using TXMLDocument

1,578 views
Skip to first unread message

Cafetorium

unread,
Jan 18, 2010, 11:09:08 AM1/18/10
to
Using Delphi 2009.

I'm not an XML expert by any means so I might be doing something wrong
here. Can anyone enlighten me?

I have a TXMLDocument on my form and it uses all the default
properties. The DOMVendor is MSXML.

In my code, I declare some variables with type IXMLNode;
eg.

var
dNode : IXMLNode;
pNode : IXMLNode;

I then create new elements in the XMLDocument like this:

dNode := pNode.AddChild('ElementName');
dNode.Attributes['AttributeName'] := someAttributeValue;
dNode.Text := someText;

This all works fine.

However, if I try this...
dNode.Text := '<![CDATA[' + someText + ']]>';

the resulting text that I get is

<ElementName>&lt;![CDATA[someText]]&gt;</ElementName>

when I was expecting

<ElementName><![CDATA[someText]]></ElementName>

Is there some way of making the content of an element a CDATA block
without getting this conversion?

I've previously written XML using writeln into text files, and of
course that worked fine, but I thought it might be better to use a
TXMLDocument to handle my XML files.

Phil

Cafetorium

unread,
Jan 18, 2010, 11:42:32 AM1/18/10
to

Just worked out how to do it.

Instead of using


dNode := pNode.AddChild('ElementName');

dNode.Text := '<![CDATA[' + someText + ']]>';

use this:
dNode := XMLDoc.CreateNode('ElementName', ntCData);
dNode.Text := someText;
pNode.ChildNodes.Add(dNode);

The CDATA delimiters are added automatically around "someText" because
I specified that the node type is ntCData.

Seems that Attributes are not supported on that node type though.
Inconvenient but not a huge problem. I'll just restructure my data.

Phil

Rob Kennedy

unread,
Jan 18, 2010, 11:45:26 PM1/18/10
to

Do you realize that in automatically generated code, cdata sections are
pointless? They're just a way to avoid doing lots of character-escaping.
Since your XML library does all the encoding for you either way, you can
just assign your raw text and it will make sure it serializes valid XML.

You've actually already observed this. When you assigned '<![CDATA[' to
the Text property, what you got was &gt;![CDATA[. Since you don't really
want the nine characters the represent to be a part of the element's
text, simply don't include them when you assign the Text property:

dNode := pNode.AddChild('ElementName');

dNode.Text := someText;

--
Rob

Maarten Wiltink

unread,
Jan 19, 2010, 7:10:23 AM1/19/10
to
"Cafetorium" <ph...@cafetorium.co.uk> wrote in message
news:647ea27b-a9db-45a7...@30g2000yqu.googlegroups.com...

[...]


> I then create new elements in the XMLDocument like this:
>
> dNode := pNode.AddChild('ElementName');
> dNode.Attributes['AttributeName'] := someAttributeValue;
> dNode.Text := someText;
>
> This all works fine.
>
> However, if I try this...
> dNode.Text := '<![CDATA[' + someText + ']]>';
>
> the resulting text that I get is
>
> <ElementName>&lt;![CDATA[someText]]&gt;</ElementName>

Yes. That's what you asked for.


> when I was expecting
>
> <ElementName><![CDATA[someText]]></ElementName>
>
> Is there some way of making the content of an element a CDATA block
> without getting this conversion?

CData blocks are text nodes. You are currently forcing a text node
inside the dNode element node by setting dNode.Text. Add an explicit
CDATA text node instead.

XML has a variety of node types that you might want to know about.
Reading a reference would probably be a good idea.

Groetjes,
Maarten Wiltink


Cafetorium

unread,
Feb 10, 2010, 8:36:20 AM2/10/10
to
On Jan 19, 12:10 pm, "Maarten Wiltink" <maar...@kittensandcats.net>
wrote:

> XML has a variety of node types that you might want to know about.
> Reading a reference would probably be a good idea.
>

yes. Good advice!
Thanks.

Phil

0 new messages