您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中用TXmlDocument定义文档类型和XML元素

时间:2011/9/3 15:29:03 点击:

  核心提示:示例代码:var sl : TStringList; xmlDoc : TXMLDocument; iNode : IXMLNode;begin xmlDoc := TXMLDocument.Crea...

示例代码:

var
   sl : TStringList;
   xmlDoc : TXMLDocument;
   iNode : IXMLNode;
begin
   xmlDoc := TXMLDocument.Create(nil) ;
   try
     xmlDoc.Active := true;

     iNode := xmlDoc.AddChild('leaf') ;
     iNode.Attributes['attrib1'] := 'value1';
     iNode.Text := 'Node Text';

     sl := TStringList.Create;
     try
       sl.Assign(xmlDoc.XML) ;

       sl.Insert(0,'<!DOCTYPE ns:mys SYSTEM "myXML.dtd">') ;
       sl.Insert(0,'<?xml version="1.0"?>') ;

       sl.SaveToFile('c:\Test.xml') ;
     finally
       sl.Free;
     end;

   finally
     xmlDoc := nil;
   end;
end;

输出XML:

<?xml version="1.0"?>
<!DOCTYPE ns:mys SYSTEM "myXML.dtd">
<leaf attrib1="value1">Node Text</leaf>

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号