【问题标题】:How do I parse this XML string in Delphi 2009?如何在 Delphi 2009 中解析这个 XML 字符串?
【发布时间】:2012-05-22 18:17:53
【问题描述】:

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

谢谢!

【问题讨论】:

  • 这有意义吗?或者这个字符串是可以解析的吗?
  • 对 xml 字符串进行了编辑,希望现在更容易理解。
  • 感谢您的帮助。好吧,我收到此字符串是为了响应 Web 服务调用。也许 Web 服务文档中的这一点信息会有所帮助。“接收响应在大多数情况下,每个 Web 服务调用的响应都以 XML 或格式化文本的形式返回。例如,上面的 URL 将返回:tempuri.org/… National Benefit Fund</Name><ApexPayerID>13162</ApexPayerID></Payer>< ... “”
  • 上述格式是我的原始字符串的样子,但在将其写入 xml 文件并抓取文本后,它看起来更像您在我当前版本中看到的。

标签: xml delphi delphi-2009 xmldocument


【解决方案1】:

您可以使用 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.

【讨论】:

  • 如果内部供应商引擎设置为 MSXML,您可以将 XPath 与 IXMLDocument 一起使用。
  • 很好,这与您提供的字符串一起工作。当我得到的字符串(响应)看起来像这样时,你知道我能做什么吗? ''#$D#$A'tempuri.org/…>' 那 '#$D#$A' 似乎把它扔掉了我很难摆脱它。
  • 整个字符串在 ... 之后没有显示,但希望你能明白发生了什么。我还将它添加到我的主要问题中。谢谢
  • 好的,那么你必须先解码你的XML,尝试使用HTTPApp单元的HTTPDecode函数,然后使用上述方法或使用Remy答案解析结果字符串。
  • 我尝试了 HTTPDecode 函数,但是一旦我加载它,它仍然没有正确读取字符串,它找不到任何节点。 :(
【解决方案2】:

XML 中的每个节点都将在IXMLDocument 中表示为IXMLNode,与它们在 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 数据绑定向导,它会生成接口来为您解析 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;

【讨论】:

  • 感谢您的帮助,我的 xml 字符串是用 html 编码的,所以没有任何效果。
猜你喜欢
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多