这是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/"><statusInfo><vendorClaimID>BRADY12478018AETNA</vendorClaimID><statusID>0</statusID><statusDescription>Unvalidated</statusDescription></statusInfo></string>'
我已将字符串加载到XMLDoc中,但不知道如何从此处轻松访问值。
var
doc: IXMLDocument;
doc := LoadXMLData(xmlString);
谢谢!
最佳答案
您可以使用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.
关于xml - 如何在Delphi 2009中解析此XML字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707740/