Delphi与XML

王朝delphi·作者佚名  2006-01-08
宽屏版  字体: |||超大  

////////////////////////////////////////////////////////////////////////

创建IXMLDOMDocument对象的三种方法:

(1)[方法1] 直接创建 IXMLDOMDocument , 例(1)

uses msxml;

var DOC:IXMLDOMDocument;

doc := CoDOMDocument.create;

.....

Doc := nil;

A)doc.load('C:\temp.xml'); //从文件载入

B) 动态创建

var aElement,aElement2: IXMLDOMElement; // [ aNode:IXMLDOMNode ==> .AppendChild() ]

//加入版本信息 ‘<?xml version="1.0" ?> ’

doc.AppendChild(doc.CreateProcessingInstruction('xml', 'version="1.0" encoding="GB2312"'));

(*)因为此函数返回结果不包含 'encoding="GB2312"' 故须保存前注意.

//加入根结点

doc.AppendChild(doc.CreateElement('bootDocNode')); //// == aElement

//加入子结点

aElement:=IXMLDOMElement(Doc.DocumentElement.AppendChild(Doc.CreateElement('ChileNode1')));

//设置接点属性

aElement.SetAttribute('ID', '11');

aElement.SetAttribute('Units', '元/m2');

//设置结点内容

aElement.AppendChild(Doc.CreateTextNode('结点内容'));

//子结点添加子结点

aElement2:=IXMLDOMElement(aElement.AppendChild(Doc.CreateElement('Child_ChileNode1')));

(2) [方法2] 直接创建 IXMLDocument ( 不是IXMLDOMDocument )

uses XMLIntf,XMLDoc;

var xmlDoc:IXMLDocument; aNode:IXMLNode; s:string;

xmlDoc := TXMLDocument.Create(nil);

try

//加入版本信息 ‘<?xml version="1.0" encoding="GB2312" ?> ’

xmlDoc.Active := True; xmlDoc.Version := '1.0'; xmlDoc.Encoding :='GB2312';

//加入根结点

aNode:=xmlDoc.AddChild('bootDocNode');

//加入子结点

aNode:=aNode.AddChild('ChileNode1');

//设置接点属性

aNode.SetAttribute('ID', '22');

aNode.SetAttribute('Units', '元/m2');

//设置结点内容

aNode.Text := '结点内容';

//子结点添加子结点

aNode:=aNode.AddChild('Child_ChileNode1') ;

aNode.Text := 'Child_ChileNod内容';

s := xmlDoc.XML.Text ; // .XML 返回的是 Tstrings

finally

xmlDoc := nil ;

end;

(3)利用 XMLDataBinding

I) 准备好XML文件,此XML文件有较强的代表性,保证程序中所用的结点及其关系都存在

II)利用 file-->new-->XML Data Binding

III)创建XML对象

A)v:string; //XML文件内容

Doc : IXMLBudgetDocTyp; //IXMLBudgetDocTyp是XML文件的根结点

Doc := LoadXMLData(v).GetDocBinding('BudgetDoc', TXMLBudgetDocType) as IXMLBudgetDocType;

B)Doc := LoadBudgetDoc('C:\temp.xml');

IV) 应用

Doc.ChildNodes.FindNode('Docfile') as IXMLDocfileType;

(4)利用TXMLDocument控件

XMLDocument1.fileName:='C:\temp.xml';

XMLDocument1.active:=true; // XMLDocument1 相当于 Doc

(5)XML对象的其他方法

IXMLNodeList.FindNode(NodeName: DOMString): IXMLNode;

IXMLNodeList.FindNode(NodeName, NamespaceURI: DOMString): IXMLNode;

IXMLNodeList.FindNode(ChildNodeType: TGuid): IXMLNode;

IXMLNodeList.FindSibling(const Node: IXMLNode; Delta: Integer): IXMLNode;

IXMLNodeList.First: IXMLNode;

IXMLNodeList.Last: IXMLNode;

... ...

////////////////////////////////////////////////////////////////////////

//例(1)

uses msxml;

doc:IXMLDOMDocument;

budgetdoc:ixmlDomNode;

Rela:IxmlDOMNode;

rs:ixmldomnodelist;

//建立或取得XML结点

doc := CoDOMDocument.create;

doc.load('C:\temp.xml');

budgetDoc := doc.selectSingleNode('BudgetDoc');

rela := budgetdoc.SelectSingleNode('Relation');

//创建XML子结点

if not assigned(rela) then

begin

rela := doc.createElement('Relation');

rela.setAttribute('BudgetId',0);

rela.setAttribute('name','名称');

budgetdoc.appendChild(rela);

end;

//取子结点(序列)

rs := rela.selectNodes('Rela[@BudgetId="2" and @TaskId="8"]');

for i := 0 to rs.length -1 do

begin

s:= s + rs[i].attributes.getnameditem('NewRate').nodeValue;

end;

rela:=doc.ChildNodes.FindNode('DOMNode') as IxmlDOMNode;

//移除子结点

for i := rs.length -1 downto 1 do

rela.removeChild(rs[i])

//取父结点

if assigned(anode.parentNode) and (Anode.parentNode.nodeName='Task') then

result := Anode.parentNode;

//取属性

DOC.DocumentElement.Attributes['Name']

rela.Attributes['Name']

rs.Nodes[i].Attributes['Name']

Ajob:ixmldomnode;jobs:ixmldomNodeList;

jobs := Ajob.selectNodes('RCJ[@Attrib="'+AAttrib+'"]')

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有