怎样编辑CDATA节点?

modou 2002-12-06 11:11:42
test.xml
<?xml version="1.0" encoding="GB2312"?>
<article>
<id>1</id>
<title>内容1</title>
<content>
<![CDATA[内容2
]]>
</content>
</article>

我这样做
<%
set xmlDoc= Server.CreateObject("microsoft.xmldom")
xmlDoc.async=false
xmlDoc.load server.MapPath("test.xml")
set node=xmlDoc.documentElement.lastChild
node.text="内容3"
''或node.text="<!CDATA["&"内容3"&"]]>"
xmlDoc.save server.MapPath("test.xml")
%>
不行,该怎么办呀?
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
modou 2002-12-07
  • 打赏
  • 举报
回复
我的test.xml文件就是用createCDATASection()生成的

我现在需要编辑CTADA中的内容,把
<![CDATA[内容2
]]>
改为
<![CDATA[内容3
]]>

这样写可以,必须先清空content节点中的内容,再添加,总感觉有其它的方法:
<%
set xmlDoc= Server.CreateObject("microsoft.xmldom")
xmlDoc.async=false
xmlDoc.load server.MapPath("test.xml")
set node=xmlDoc.documentElement.lastChild
node.text=""
node.appendChild xmlDoc.createCDATASection("内容3")
xmlDoc.save server.MapPath("test.xml")
%>

孟版主,帮忙写一段代码吧,谢了
孟子E章 2002-12-07
  • 打赏
  • 举报
回复
用createCDATASection()方法:
createCDATASection Method [Script]
Creates a CDATA section node that contains the supplied data.

[Script]
Script Syntax
var objXMLDOMCDATASection = oXMLDOMDocument.createCDATASection(data);
Parameters
data
A string specifying the value to be supplied to the new IXMLDOMCDATASection object's nodeValue property.
Return Value
An object. Returns the new IXMLDOMCDATASection object.

Example
The following script example creates a new CDATA section node and appends it as the last child node of the DOMDocument object.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
var root;
var CDATASection;
xmlDoc.async = false;
xmlDoc.load("books.xml");
root = xmlDoc.documentElement;
CDATASection = xmlDoc.createCDATASection("Hello World!");
root.appendChild(CDATASection);
alert(root.xml);
[Visual Basic]
Visual Basic Syntax
Set objXMLDOMCDATASection = oXMLDOMDocument.createCDATASection(data)
Parameters
data
A string specifying the value to be supplied to the new CDATASection object's nodeValue property.
Return Value
An object. Returns the new IXMLDOMCDATASection object.

Example
The following Visual Basic example creates a new CDATA section node and appends it as the last child node of the DOMDocument object.

Dim xmlDoc As New Msxml2.DOMDocument
Dim root As IXMLDOMElement
Dim CDATASection As IXMLDOMCDATASection
xmlDoc.async = False
xmlDoc.Load ("books.xml")
Set root = xmlDoc.documentElement
Set CDATASection = xmlDoc.createCDATASection("Hello World!")
root.appendChild CDATASection
MsgBox (root.xml)
[C/C++]
C/C++ Syntax
HRESULT createCDATASection(
BSTR data,
IXMLDOMCDATASection **cdata);
Parameters
data [in]
The value to be supplied to the new CDATA section object's nodeValue property.
cdata [out, retval]
The address of the new IXMLDOMCDATASection.
C/C++ Return Values
S_OK
The value returned if successful.
E_INVALIDARG
The value returned if cdata is Null.
E_FAIL
The value returned if an error occurs.
Remarks
Creating a CDATA section with this method is the same as using createNode where the type parameter value is NODE_CDATA_SECTION and no namespace is specified. You cannot specify a namespace with the createCDATASection method.

Although this method creates the new object in the context of this document, it does not automatically add the new object to the document tree. In other words, although the ownerDocument property of the new node points to this document object, the parentNode property is set to Null. To add the new object, you must explicitly call one of the node insert methods, insertBefore method, replaceChild method, or appendChild method.

The new object's nodeType property has the value NODE_CDATA_SECTION.
modou 2002-12-06
  • 打赏
  • 举报
回复
test.xml
<?xml version="1.0" encoding="GB2312"?>
<article>
<id>1</id>
<title>内容1</title>
<content>
<![CDATA[内容2
]]>
</content>
</article>

我这样做
<%
set xmlDoc= Server.CreateObject("microsoft.xmldom")
xmlDoc.async=false
xmlDoc.load server.MapPath("test.xml")
set node=xmlDoc.documentElement.lastChild
node.text="内容3"
''或node.text="<!CDATA["&"内容3"&"]]>"
xmlDoc.save server.MapPath("test.xml")
%>
不行,该怎么办呀?

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧