我需要从XML读取状态字段的帮助

时间:2018-08-04 21:24:44

标签: xml delphi delphi-7

我需要使用Delphi 7阅读此XML文本中的status字段。

此XML位于TMemo内:

<?xml version="1.0" encoding="UTF-8"?>
<licensecloud>
     <request_id>665675780</request_id>
     <request_timestamp>8/4/2018 15:47:44</request_timestamp>
     <request_status>OK</request_status>
     <error_num>0</error_num>
     <error_msg></error_msg>
     <error_desc></error_desc>
     <license>
          <key>a6gdfg567c79afd429d872d</key>
          <dashed>A6589-dfg46D429-D872D</dashed>
          <status>ACTIVE</status>
<trial>no</trial>
<expires/>

到目前为止,我这样做没有任何结果,我什么也没得到:

Uses XMLDoc, XMLIntf;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  XmlFile : TXMLDocument;
  Doc: IXMLDocument;
  MainNode, CustomerNode : IXMLNode;
  i : Integer;
  XMLPath : string;
begin
  Doc := LoadXMLData(Memo1.Text);
  try
   // XmlFile.LoadFromFile(XMLPath);
    Doc.Active := True;
    MainNode := Doc.DocumentElement;

    for i:=0 to MainNode.ChildNodes['licensecloud'].ChildNodes.Count-1 do
    begin
      CustomerNode := MainNode.ChildNodes['licensecloud'].ChildNodes[i];
      //Here you can get any imformation
      ShowMessage(CustomerNode.ChildNodes['status'].Text);

      //ShowMessage(CustomerNode.ChildNodes['address_line_1'].Text);
    end;
  finally
    FreeAndNil(XmlFile);
  end;
end;

1 个答案:

答案 0 :(得分:2)

  1. 您的xml无效。缺少结束标记(</license></licensecloud>)。有效的xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <licensecloud>
        <request_id>665675780</request_id>
        <request_timestamp>8/4/2018 15:47:44</request_timestamp>
        <request_status>OK</request_status>
        <error_num>0</error_num>
        <error_msg></error_msg>
        <error_desc></error_desc>
        <license>
            <key>a6gdfg567c79afd429d872d</key>
            <dashed>A6589-dfg46D429-D872D</dashed>
            <status>ACTIVE</status>
        </license>
        <trial>no</trial>
        <expires/>
    </licensecloud>
    
  2. MainNode为<licensecloud>MainNode.ChildNodes['licensecloud']不正确。正确的代码:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      XmlFile : TXMLDocument;
      Doc: IXMLDocument;
      MainNode, CustomerNode : IXMLNode;
      i : Integer;
      XMLPath : string;
    begin
    
    
      Doc := LoadXMLData(Memo1.Text);
      try
       // XmlFile.LoadFromFile(XMLPath);
        Doc.Active := True;
        MainNode := Doc.DocumentElement;
    
        ShowMessage(MainNode.ChildNodes['license'].ChildNodes['status'].Text);
      finally
        FreeAndNil(XmlFile);
      end;
    end;