3

我需要解析以下 XML:

<?xml version="1.0" encoding="UTF-8" ?>
<revista numero="2226" data="03/09/2013">
  <processo numero="902987089">
    <despachos>
      <despacho codigo="IPAS139"/>
    </despachos>
    <titulares>
      <titular nome-razao-social="AAAAA" pais="BR" uf="PR"/>
    </titulares>
  </processo>
  <processo numero="902812165">
    <despachos>
      <despacho codigo="IPAS029"/>
    </despachos>
    <titulares>
      <titular nome-razao-social="XXXX" pais="BR" uf="SC"/>
    </titulares>
(...)

我对 XML 一点经验都没有。我IXMLDocument在德尔福使用。

LNodeElement := LDocument.ChildNodes.FindNode('revista');
(...)
for I := 0 to LNodeElement.ChildNodes.Count - 1 do
(...)

我的问题是如何达到标签numero内的属性值?<processo>但是,如果一个善良的灵魂可以分享一个小例子,那将不胜感激。

4

1 回答 1

5

例如这种方式:

uses
  XMLDoc, XMLIntf;

procedure TForm3.Button1Click(Sender: TObject);
var
  I: Integer;
  NumberAttr: IXMLNode;
  XMLDocument: IXMLDocument;
  ProcessNodes: IXMLNodeList;
begin
  // load an XML file
  XMLDocument := LoadXMLDocument('c:\File.xml');
  // take the list of all "revista/processo" nodes
  ProcessNodes := XMLDocument.DocumentElement.ChildNodes;
  // and iterate that "processo" node collection
  for I := 0 to ProcessNodes.Count - 1 do
  begin
    // try to find the "numero" attribute for currently iterated "processo" node
    NumberAttr := ProcessNodes[I].AttributeNodes.FindNode('numero');
    // if the "numero" attribute was found, show its value (or do something else)
    if Assigned(NumberAttr) then
      ShowMessage(NumberAttr.Text);
  end;
end;
于 2013-09-05T12:11:21.957 回答