Delphi Traversing An XML Nodes (DOM)

Asked

Viewed 1,390 times

1

I have the code below, but I’m not able to list the node name and its value. I can search and show the main node type [ide] but the sub-node that would be [Cuf] + value that I can’t.

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 the 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]
  • Wouldn’t have to follow the same logic of the search for ide putting two bars before? That way: Doc.selectNodes('//ide').item[i].selectSingleNode('//cUF').text

  • I already did. But it does not go to the next modules, I needed to loop and pass Knot to Knot, thing that is not doing. And for some reason I can’t catch the name of the knot.

  • So I don’t understand your question, you want to sweep all knots from the knot ide?

  • If possible, add in question the desired output.

  • That’s right, open the XML put in a loop and go through the nodes. I’m playing the test level values for a memo, but I’m going to play these values for a table... Example I need to go through the xml starting the <Cfe> node, to get the subnodes and their values.

1 answer

1

Apparently the property length of IXMLDOMNodeList not working properly. The solution then was to use the function nextNode until she returns nil. After getting the node just sweep all the 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 the Delphi 5 installed on my machine, I performed these tests in Delphi 2010, but I believe that there are not many differences. Could not load either XML with the tags involved by [] replaced by <>.

  • Thank you very much. I put brackets, because here I can not post the codes.

  • Beauty. If the answer has been helpful, give it its due value. Accept it or give one up, or the two actions :D

  • I’m trying to score you, like I do ?

  • Take a look at the tour, in it is explained how to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.