Solved

How to Insert CDATA in XML file

Posted on 2003-12-12
3
1,698 Views
Last Modified: 2012-05-05
Hi All,

I'm creating XML file using XMLDOM object and VB script. I want all my XML node value to be embedded within the CDATA.

For example here in the following XML file the <enum> node values are embedded inside the CDATA.
---------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<test>
        <enum><![CDATA[test data]]></enum>
        <enum><![CDATA[test data1]]></enum>
</test>
---------------------------------------------------------------------------------------

 Is there any property I need to set to do the same or explicitly I have add the this "![CDATA[]]" while creating the xml node ?

Thanks,
Lilu.

0
Comment
Question by:lilupat
  • 2
3 Comments
 
LVL 15

Expert Comment

by:deighc
ID: 9927096
You can specify a node type parameter when you use the createNode method. 4 is the value for a CDATA node. Assuming you have an XML DOM object called xmlDOMObj:

set cdataNode = xmlDOMObj.createNode("nodeName", 4, "<namespace's in here if you want>")
0
 
LVL 28

Expert Comment

by:sybe
ID: 9928376
this code will create a datasection in a node

<%
' oXML is an XML-document (object)
' oNode is a Node of the XMLdocument

Set oDataNode = oXML.createCDATASection("your data here")
oNode.appendChild(oDataNode)
Set oDataNode = Nothing
%>
0
 
LVL 15

Accepted Solution

by:
deighc earned 100 total points
ID: 9928436
Small mistake in my first post: I had the arguments for the createNode method round the way.

set cdataNode = xmlDOMObj.createNode("nodeName", 4, "<namespace's in here if you want>")

should actually be

set cdataNode = xmlDOMObj.createNode(4, "nodeName", "<namespace's in here if you want>")

The suggestions from Sybe and I are functionally identical - in the same way that the createAttribute method achieves the same thing as using createNode(2, "nodeName", "").

The only difference is that the createNode method allows you to specify namespaces if you need them. createAttribute, createCDATASection etc don't allow you to add namespaces.

But Sybe's code should do the trick too.

[I guess the advantage to using createAttribute, createCDATASection etc is that it's more instantly obvious what's happening in your code when you look at it].
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now