XML 中的每个节点都将表示为IXMLNode
中的IXMLDocument
,与它们在 XML 中出现的层次结构相同。如果您首先查看带有缩进节点的 XML 会有所帮助,这样您就可以更清楚地看到层次结构:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<statusInfo>
<vendorClaimID>BRADY12478018AETNA</vendorClaimID>
<statusID>0</statusID>
<statusDescription>Unvalidated</statusDescription>
</statusInfo>
</string>
如果您了解层次结构,则可以为其编写代码:
var
doc: IXMLDocument;
statusInfo: IXMLNode;
vendorClaimID: String;
statusID: Integer;
statusDescription: String;
begin
doc := LoadXMLData(xmlString);
statusInfo := doc.DocumentElement.ChildNodes['statusInfo'];
vendorClaimID := statusInfo.ChildNodes['vendorClaimID'].Text;
statusID := StrToInt(statusInfo.ChildNodes['statusID'].Text);
statusDescription := statusInfo.ChildNodes['statusDescription'].Text;
end;
或者:
var
doc: IXMLDocument;
statusInfo: IXMLNode;
vendorClaimID: String;
statusID: Integer;
statusDescription: String;
begin
doc := LoadXMLData(xmlString);
statusInfo := doc.DocumentElement.ChildNodes['statusInfo'];
vendorClaimID := VarToStr(statusInfo.ChildValues['vendorClaimID']);
statusID := StrToInt(VarToStr(statusInfo.ChildValues['statusID']));
statusDescription := VarToStr(statusInfo.ChildValues['statusDescription']);
end;
如果您使用 Delphi 的 XML Data Binding 向导,它将生成将为您解析 XML 的接口:
var
doc: IXMLDocument;
statusInfo: IXMLstatusInfoType;
vendorClaimID: String;
statusID: Integer;
statusDescription: String;
begin
doc := LoadXMLData(xmlString);
statusInfo := Getstring(doc).statusInfo;
vendorClaimID := statusInfo.vendorClaimID;
statusID := statusInfo.statusID;
statusDescription := statusInfo.statusDescription;
end;