xml增加节点并保存到文件的一些问题(100分)

  • 主题发起人 linuxping
  • 开始时间
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
以下函数实现创建节点,然后把Doc保存到文件.但文件里并没有出现新创建的节点:
procedure TCutedFile.AddCutedFile(ACutedPath,ACutedSavePath,ASavetoPath: string;mc:TMethodChoose);
var
do
c:IDOMDocument;
xmlNode,xmlSubNode:IDOMNode;
xmlAttr:IDOMNode;
begin
FXMLDoc.Active:=True;
do
c:=FXMLDoc.DOMDocument;
xmlNode:=Doc.childNodes.item[1];
//xmlNode='CutWord' node.
xmlNode:=xmlNode.appendChild(Doc.createElement('File'));
// xmlnode= 'File' node.
//ShowMessage(xmlNode.nodeName);
xmlAttr:=Doc.createAttribute('scrpath');
//属性节点(xmlAttr='scrpath' node)
xmlAttr.NodeValue:=ACutedPath;
//
xmlNode.attributes.setNamedItem(xmlAttr);

xmlSubNode:=Doc.createElement('Cuted');
xmlNode:=xmlNode.appendChild(xmlSubNode);
//xmlnode='Cuted' node.

xmlAttr:=Doc.createAttribute('time');
//属性节点
xmlAttr.NodeValue:=DateToStr(Date);
xmlNode.attributes.setNamedItem(xmlAttr);
xmlAttr:=Doc.createAttribute('direction');
//属性节点
xmlAttr.NodeValue:=McValue(mc);
xmlNode.attributes.setNamedItem(xmlAttr);
xmlNode.appendChild(Doc.createTextNode(ACutedSavePath));
//添加文本
FXMLDoc.Active:=False;
FXMLDoc.Active:=True;
//FXMLDoc.Resync;
//ShowMessage(FXMLDoc.XML.Text);
ForceDomTofile(ASavetoPath);
end;
procedure TCutedFile.ForceDomTofile(APath:string);//Dom保存到文件
var
strmMemo:TMemoryStream;
strmString:TStringStream;
sEncry:string;
begin
FXMLDoc.NodeIndentStr:=#32#32;
sEncry:=FSecret.EncryptString(FormatXMLData(FXMLDoc.XML.Text),xmlKEY);//这是个加密函数
strmString:=TStringStream.Create(sEncry);
try
strmMemo:=TMemoryStream.Create;
try
strmMemo.LoadFromStream(strmString);
strmMemo.SaveToFile(APath);
finally
strmMemo.Free;
end;
finally
strmString.Free;
end;

end;
 
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
xml文件结构如下:
<?xml version=&quot;1.0&quot;
encoding=&quot;UTF-8&quot;?>
<CutWord>
<File scrpath=&quot;C:/Documents and Settings/new/(2).txt&quot;>

<Cuted direction=&quot;Order&quot;
time=&quot;&quot;>C:/Documents and Settings/new/aa.txt </Cuted>
</File>
<File scrpath=&quot;C:/Documents and Settings/new/bbb.txt&quot;>
<Cuted direction=&quot;Reveser&quot;
Time=&quot;&quot;>C:/Documents and Settings/new/bbb1.txt </Cuted>
</File>
<File scrpath=&quot;F:/Delphi.TXT&quot;>
<Cuted direction=&quot;Order&quot;
time=&quot;&quot;>F:/Delphi</Cuted>
<Cuted direction=&quot;Reveser&quot;
time=&quot;&quot;>F:/Delphi/1.txt</Cuted>
<Cuted direction=&quot;Probability&quot;
Time=&quot;&quot;>C:/Documents and Settings/new/bbb1.txt </Cuted>
<Cuted direction=&quot;Integrate&quot;
Time=&quot;&quot;>C:/Documents and Settings/new/bbb1.txt </Cuted>
</File>

</CutWord>
 
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
大家也发表下意见!
文件里为什么没有出现新创建的节点??????????
 
X

xiammy

Unregistered / Unconfirmed
GUEST, unregistred user!
createElement是创建Xml的根节点,我估计你这样应该用
CreateNode吧?
另外,你为什么一直用DOMDocumentd的创建方法啊?Node也是有方法的。
 
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
to xiammy:
IDOMDocument没有CreateNode方法.不过createTextNode方法倒有.
createElement中文该翻译成创建元素吧,不只是创建根节点
createElement除了可以创建各种类型的Xml节点外,createElement创建的文本节点与createTextNode创建的文本节点不知道有什么不同.
 
N

newsmile

Unregistered / Unconfirmed
GUEST, unregistred user!
参考 http://www.delphibbs.com/delphibbs/dispq.asp?lid=756252
帖子有点古老啊,不过应该可以解决你的问题。
 
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
thanks
不过,我的程序的问题还没找出来....分还不能发啊
 
L

linuxping

Unregistered / Unconfirmed
GUEST, unregistred user!
无奈结贴!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
598
import
I
S
回复
0
查看
814
SUNSTONE的Delphi笔记
S
顶部