如何在Delphi 2009中解析此XML字符串? [英] How do I parse this XML string in Delphi 2009?

查看:83
本文介绍了如何在Delphi 2009中解析此XML字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是XML字符串具有的信息。

This is the information that the XML string has.

<?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中,但是不知道如何轻松访问值

I have loaded the string into a XMLDoc, but don't know how to access the values easily from here..

var
doc: IXMLDocument;


doc := LoadXMLData(xmlString);

谢谢!

推荐答案

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

You can use XPath to extract the values of the nodes

检查此示例

{$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.

这篇关于如何在Delphi 2009中解析此XML字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆