1

这是 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>

但这就是它的来源。您必须向右滚动才能看到所有内容。

'<?xml version="1.0" encoding="utf-8"?>'#$D#$A'<string xmlns="http://tempuri.org/">&lt;statusInfo&gt;&lt;vendorClaimID&gt;BRADY12478018AETNA&lt;/vendorClaimID&gt;&lt;statusID&gt;0&lt;/statusID&gt;&lt;statusDescription&gt;Unvalidated&lt;/statusDescription&gt;&lt;/statusInfo&gt;</string>'

我已将字符串加载到 XMLDoc 中,但不知道如何从这里轻松访问这些值。

var
doc: IXMLDocument;


doc := LoadXMLData(xmlString);

谢谢!

4

2 回答 2

7

您可以使用 XPath 提取节点的值

检查这个样本

{$APPTYPE CONSOLE}

{$R *.res}

uses
  MSXML,
  SysUtils,
  ActiveX,
  ComObj;


Const

XMLStr=
'<?xml version="1.0" encoding="UTF-8"?> '+
'<string xmlns="http://tempuri.org/">'+
' <statusInfo>'+
'  <vendorClaimID>BRADY12478018AETNA</vendorClaimID> '+
'  <statusID>0</statusID><statusDescription>Unvalidated</statusDescription> '+
' </statusInfo>'+
'</string> ';

procedure Test;
Var
  XMLDOMDocument  : IXMLDOMDocument;
  XMLDOMNode      : IXMLDOMNode;
begin
  XMLDOMDocument:=CoDOMDocument.Create;
  XMLDOMDocument.loadXML(XmlStr);
  XMLDOMNode := XMLDOMDocument.selectSingleNode('//string/statusInfo/vendorClaimID');
  if XMLDOMNode<>nil then
    Writeln(Format('vendorClaimID %s',[String(XMLDOMNode.Text)]));

  XMLDOMNode := XMLDOMDocument.selectSingleNode('//string/statusInfo/statusID');
  if XMLDOMNode<>nil then
    Writeln(Format('statusID %s',[String(XMLDOMNode.Text)]));

  XMLDOMNode := XMLDOMDocument.selectSingleNode('//string/statusInfo/statusDescription');
  if XMLDOMNode<>nil then
    Writeln(Format('statusDescription %s',[String(XMLDOMNode.Text)]));
end;


begin
 try
    CoInitialize(nil);
    try
      Test;
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
于 2012-05-22T23:10:59.010 回答
4

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;
于 2012-05-22T23:38:48.590 回答