Delphi Browsing the Nodes of an XML (DOM)

0

I have the following code, but I am not able to list the node name and its value. I can search and show the main node [ide] but the sub-node that would be [cUF] + value this I can not.

procedure TForm1.Button3Click(Sender: TObject);  
var
    Doc: IXMLDOMDocument;  
    Element: IXMLDOMElement;  
    List: IXMLDOMNodeList;  
    attrib : IXMLDOMNode;  
    Path,ID,title,producer: string;  
    i: integer;  
begin    
    Memo1.Lines.Clear;
    Doc := CoDOMDocument.Create;
    Doc.load(ArquivoXML);
    // Element := Doc.documentElement;
    // List := Element.getElementsByTagName('ide');
    for i := 0 to Doc.selectNodes('//ide').length do
    begin
        Memo1.Lines.Add(Doc.selectNodes('//ide').item[i].baseName);     // Nó onde está parado
        Memo1.Lines.Add(Doc.selectNodes('//ide').item[i].Get_nodeName); // = basename
        Memo1.Lines.Add(Doc.selectNodes('//ide').item[i].nodeName);     // = basename
        Memo1.Lines.Add(Doc.selectNodes('//ide').item[i].selectSingleNode('cUF').text);   // Pego o Conteúdo do item do Nó
        Memo1.Lines.Add(Doc.selectNodes('//ide').item[i].selectSingleNode('cNF').text);   // Pego o Conteúdo do item do Nó

//        Memo1.Lines.Add(IntToStr(GetNodeInt(attrib,'cNF')));
    end;
end;

Excerpt from XML :

[CFe]  
    [infCFe versaoSB="000003" versaoDadosEnt="0.07" versao="0.07"  
    Id="CFeNumero_da_chave"]  
        [ide]  
            [cUF]35[/cUF]  
            [cNF]164796[/cNF]  
            [mod]59[/mod]  
            [nserieSAT]123456789[/nserieSAT]  
            [nCFe]000289[/nCFe]  
            [dEmi]20181029[/dEmi]  
            [hEmi]100256[/hEmi]  
            [cDV]3[/cDV]  
            [tpAmb]2[/tpAmb]  
            [CNPJ]11111111111111[/CNPJ]  
            [signAC]assinatura[/signAC]  
            [assinaturaQRCODE]QRCode[/assinaturaQRCODE]  
            [numeroCaixa]002[/numeroCaixa]  
        [/ide]  
    [/infCFe"  
[/CFe]
    
asked by anonymous 05.11.2018 / 14:35

1 answer

0

Apparently the length property of IXMLDOMNodeList is not working properly. The solution was then to use the nextNode function until it returns nil . After getting the node just scan all child nodes and print them:

procedure TForm1.Button3Click(Sender: TObject);
var
    Doc: IXMLDOMDocument;
    nodes: IXMLDOMNodeList;
    node : IXMLDOMNode;
    i: integer;
begin
    Memo1.Lines.Clear;
    Doc := CoDOMDocument.Create;
    Doc.loadXML(
      '<CFe>                                                                                      ' +
      '    <infCFe versaoSB="000003" versaoDadosEnt="0.07" versao="0.07" Id="CFeNumero_da_chave"> ' +
      '        <ide>                                                                              ' +
      '            <cUF>35</cUF>                                                                  ' +
      '            <cNF>164796</cNF>                                                              ' +
      '            <mod>59</mod>                                                                  ' +
      '            <nserieSAT>123456789</nserieSAT>                                               ' +
      '            <nCFe>000289</nCFe>                                                            ' +
      '            <dEmi>20181029</dEmi>                                                          ' +
      '            <hEmi>100256</hEmi>                                                            ' +
      '            <cDV>3</cDV>                                                                   ' +
      '            <tpAmb>2</tpAmb>                                                               ' +
      '            <CNPJ>11111111111111</CNPJ>                                                    ' +
      '            <signAC>assinatura</signAC>                                                    ' +
      '            <assinaturaQRCODE>QRCode</assinaturaQRCODE>                                    ' +
      '            <numeroCaixa>002</numeroCaixa>                                                 ' +
      '        </ide>                                                                             ' +
      '        <ide>                                                                              ' +
      '            <cUF>36</cUF>                                                                  ' +
      '            <cNF>164797</cNF>                                                              ' +
      '            <mod>60</mod>                                                                  ' +
      '            <nserieSAT>123456790</nserieSAT>                                               ' +
      '            <nCFe>000290</nCFe>                                                            ' +
      '            <dEmi>20181030</dEmi>                                                          ' +
      '            <hEmi>100257</hEmi>                                                            ' +
      '            <cDV>4</cDV>                                                                   ' +
      '            <tpAmb>3</tpAmb>                                                               ' +
      '            <CNPJ>11111111111112</CNPJ>                                                    ' +
      '            <signAC>assinatura1</signAC>                                                   ' +
      '            <assinaturaQRCODE>QRCode1</assinaturaQRCODE>                                   ' +
      '            <numeroCaixa>003</numeroCaixa>                                                 ' +
      '        </ide>                                                                             ' +
      '    </infCFe>                                                                              ' +
      '</CFe>                                                                                     '
    );

    nodes := Doc.getElementsByTagName('ide');

    repeat
      node := nodes.nextNode;

      if (node = nil) then
        Break;

      Memo1.Lines.Add(node.baseName);
      for i := 0 to node.childNodes.length - 1 do
        Memo1.Lines.Add('[' + node.childNodes.item[i].nodeName + '] ' + node.childNodes.item[i].text);
    until (node = nil);
end;

The result of this was:

ide
[cUF] 35
[cNF] 164796
[mod] 59
[nserieSAT] 123456789
[nCFe] 000289
[dEmi] 20181029
[hEmi] 100256
[cDV] 3
[tpAmb] 2
[CNPJ] 11111111111111
[signAC] assinatura
[assinaturaQRCODE] QRCode
[numeroCaixa] 002
ide
[cUF] 36
[cNF] 164797
[mod] 60
[nserieSAT] 123456790
[nCFe] 000290
[dEmi] 20181030
[hEmi] 100257
[cDV] 4
[tpAmb] 3
[CNPJ] 11111111111112
[signAC] assinatura1
[assinaturaQRCODE] QRCode1
[numeroCaixa] 003

I used the following XML as an example:

<CFe>                                                                                      
    <infCFe versaoSB="000003" versaoDadosEnt="0.07" versao="0.07" Id="CFeNumero_da_chave"> 
        <ide>                                                                              
            <cUF>35</cUF>                                                                  
            <cNF>164796</cNF>                                                              
            <mod>59</mod>                                                                  
            <nserieSAT>123456789</nserieSAT>                                               
            <nCFe>000289</nCFe>                                                            
            <dEmi>20181029</dEmi>                                                          
            <hEmi>100256</hEmi>                                                            
            <cDV>3</cDV>                                                                   
            <tpAmb>2</tpAmb>                                                               
            <CNPJ>11111111111111</CNPJ>                                                    
            <signAC>assinatura</signAC>                                                    
            <assinaturaQRCODE>QRCode</assinaturaQRCODE>                                    
            <numeroCaixa>002</numeroCaixa>                                                 
        </ide>                                                                             
        <ide>                                                                              
            <cUF>36</cUF>                                                                  
            <cNF>164797</cNF>                                                              
            <mod>60</mod>                                                                  
            <nserieSAT>123456790</nserieSAT>                                               
            <nCFe>000290</nCFe>                                                            
            <dEmi>20181030</dEmi>                                                          
            <hEmi>100257</hEmi>                                                            
            <cDV>4</cDV>                                                                   
            <tpAmb>3</tpAmb>                                                               
            <CNPJ>11111111111112</CNPJ>                                                    
            <signAC>assinatura1</signAC>                                                   
            <assinaturaQRCODE>QRCode1</assinaturaQRCODE>                                   
            <numeroCaixa>003</numeroCaixa>                                                 
        </ide>                                                                             
    </infCFe>                                                                              
</CFe>                                                                                     

I do not have Delphi 5 installed on my machine, I ran those tests in Delphi 2010, but I do not think there are many differences. It also could not load XML with tags involved by [] instead of <> .

    
05.11.2018 / 15:41