Delphi使用IXMLDOMDocument和IXMLDomNode从XML读取struct中的参数;

时间:2014-02-18 10:30:53

标签: xml delphi parsing

我有这个xml文件:

<?xml version='1.0'?>
<methodResponse>
    <params>
        <param>
            <value><struct>
                    <member>
                        <name>status</name>
                        <value><int>1</int></value>
                    </member>
                    <member>
                        <name>msg</name>
                        <value><string>Operazione completata correttamente</string></value>
                    </member>
                    <member>
                        <name>valres</name>
                        <value><struct>
                                <member>
                                    <name>coderr</name>
                                    <value><int>0</int></value>
                                </member>
                                <member>
                                    <name>deserr</name>
                                    <value><string>ricezione di UpdateDevice avvenuta correttamente</string></value>
                                </member>
                                <member>
                                    <name>pairingip</name>
                                    <value><string>192.168.1.15</string></value>
                                </member>
                                <member>
                                    <name>pairingdevtype</name>
                                    <value><int>3</int></value>
                                </member>
                            </struct></value>
                    </member>
                    <member>
                        <name>svrvers</name>
                        <value><string></string></value>
                    </member>
                </struct></value>
        </param>
    </params>
</methodResponse>

但如果它存在,我无法阅读pairingip和pairingdevtype。我该怎么办?

我试过了:

procedure TForm1.ParseResult(ResultString : string);
var
  xml: IXMLDOMDocument;
  node: IXMLDomNode;
  nodes_row, nodes_se: IXMLDomNodeList;
  i, j: Integer;
  url: string;

begin
  xml := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
  xml.async := False;
  xml.loadXML(Resultstring); // or use loadXML to load XML 
  if xml.parseError.errorCode <> 0 then
    raise Exception.Create('XML Load error:' + xml.parseError.reason);

  Node := xml.selectSingleNode('//methodResponse/params/param');
  if (Node<>nil) then exit;

  nodes_row := xml.selectNodes('//methodResponse/params/param');
  for i := 0 to nodes_row.length - 1 do
  begin
    node := nodes_row.item[i];
    Memo2.Lines.Add('res=' + node.text);
  end;
end;

在这里,我可以在一行中获得所有param的内容,但我可以去阅读pairingip和pairingdevtype

请帮帮我

1 个答案:

答案 0 :(得分:2)

这是一种方法,因为您的XML清楚地表明您正在寻找的两个值始终位于特定级别的某个位置:

procedure TForm1.Button1Click(Sender: TObject);
var
  XMLDoc: IXMLDOMDocument;
  Node, SibNode: IXMLDOMNode;
begin
  Memo2.Lines.Clear;  // Just for test output

  XMLDoc := CoDOMDocument.Create;
  XMLDoc.LoadXML(Memo1.Lines.Text);  // XML added to Memo1.Lines

  // Find the member/name node with the text 'pairingip'
  Node := XMLDoc.selectSingleNode('//member/name[text()="pairingip"]');

  if Assigned(Node) then
  begin
    SibNode := Node.nextSibling.firstChild;  // Steps to the 'value/string' node
    Memo2.Lines.Add('IP: ' + SibNode.Text);
  end;

  // Find the member/name node with the text 'pairingdevtype'
  Node := XMLDoc.selectSingleNode('//member/name[text()="pairingdevtype"]');
  if Assigned(Node) then
  begin
    SibNode := Node.nextSibling.firstChild;  // Steps to the 'value/int' node
    Memo2.Lines.Add('DevType: ' + SibNode.Text);
  end;
end;