Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取XML中的值_Xml_Delphi_Delphi Xe - Fatal编程技术网

提取XML中的值

提取XML中的值,xml,delphi,delphi-xe,Xml,Delphi,Delphi Xe,我需要解析以下XML: <?xml version="1.0" encoding="UTF-8" ?> <revista numero="2226" data="03/09/2013"> <processo numero="902987089"> <despachos> <despacho codigo="IPAS139"/> </despachos> <titulares>

我需要解析以下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>
(...)
我的问题是如何到达
标记内的
numero
属性值?但是,如果一个善良的灵魂能分享一个小小的例子,那将是最值得感激的。

例如:

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;

等等,但这是错的,不是吗?您将获得
标记的值,是吗?我正在迭代
标记。。。你错过了句子中最重要的一个词,我如何才能达到标记中的“numero”值,但是那里缺少了哪个标记。
numero
属性在两个标记中,在
以及
标记中都存在。它是正确的。我需要里面的头号人物。第一个是不需要的。啊,对不起,它在问题中,因为它是一个没有引号的标签。嗯,这一次我很幸运:-)@MiguelE:我知道你已经接受了答案,但是你可以做的另一件事是编写一个XSD文件,并使用Delphi XML数据绑定向导生成一个实用程序单元来处理XML的所有本质。编写一个XSD文件很容易,在中有很好的解释。通过这样做,你的应用程序的代码会简单得多。你需要更深一层。您试图读取的是
节点中的
codigo
属性,而不是
中的属性。您也可以使用
FindNode
完成此操作,如您在问题中所示(请参阅)。我在这里使用了节点列表,只是因为我要迭代相同级别的所有
节点。
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;