Delphi Problems When Listing XML Sub-Node

Asked

Viewed 228 times

0

I have the code below, but at the node of [ICMS]. he puts it in anyway line I’m not getting to read the children. The same is happening with the [PIS] and [COFINS].* It does not obey level 2. How to pick up or check if there are more levels or knots? I thank anyone who can help...

procedure TForm1.Button1Click(Sender: TObject);
var Doc: IXMLDOMDocument;
    node_pai,
    node_filho: IXMLDOMNode;
    node_lista: IXMLDOMNodeList;
    i, j: Integer;
begin
    Memo1.Lines.Clear;
    Doc := CoDOMDocument.Create;
    Doc.load(

'<imposto>                        '+
'    <vItem12741>8.25</vItem12741>'+
'    <ICMS>                       '+
'       **<ICMS00>**              '+
'           <Orig>0</Orig>        '+
'           <CST>00</CST>         '+
'           <pICMS>18.00</pICMS>  '+ 
'           <vICMS>4.53</vICMS>   '+ 
'       </ICMS00>                 '+ 
'   </ICMS>                       '+
'   <PIS>                         '+
'       <PISAliq>                 '+ 
'           <CST>01</CST>         '+
'           <vBC>25.15</vBC>      '+
'           <pPIS>0.0000</pPIS>   '+
'           <vPIS>0.00</vPIS>     '+
'       </PISAliq>                '+
'   </PIS>                        '+
'   <COFINS>                      '+
'       <COFINSAliq>              '+
'           <CST>01</CST>         '+
'           <vBC>25.15</vBC>      '+ 
'           <pCOFINS>0.0000</pCOFINS>'+
'           <vCOFINS>0.00</vCOFINS>  '+
'       </COFINSAliq>                '+ 
'   </COFINS>                        '+
'</imposto>                          '          
);
    Doc.async  := False; 
    node_lista := Doc.selectNodes('//'+Edit1.Text);

    repeat
        node_pai := node_lista.nextNode;
        if (node_pai = nil) then
            Break;

        Memo1.Lines.Add(node_pai.baseName);
        // Nó do Pai ou Nível 1
        for i := 0 to node_pai.childNodes.length -1 do
        begin
            node_filho := node_pai.childNodes[i];
            if (node_filho <> nil) and 
               (node_pai.childNodes[i].childNodes.length > 1) then
            begin
                // Nó do filho ou Nível 2
                for j := 0 to node_pai.childNodes[i].childNodes.length -1 do
                   Memo1.Lines.Add('<' + 
                   node_pai.childNodes[i].childNodes.item[j].nodeName + '> ' 
                   + node_pai.childNodes[i].childNodes.item[j].text);
            end
            else
               // Nó do Pai ou Nível 1
               Memo1.Lines.Add('<' + node_pai.childNodes.item[i].nodeName + 
               '> ' + node_pai.childNodes.item[i].text);
        end;
    until (node_pai = nil);
end;

Excerpt from the XML

<imposto>
    <vItem12741>8.25</vItem12741>
    <ICMS>
        <ICMS00>
            <Orig>0</Orig>
            <CST>00</CST>
            <pICMS>18.00</pICMS>
            <vICMS>4.53</vICMS>
        </ICMS00>
    </ICMS>
    <PIS>
        <PISAliq>
            <CST>01</CST>
            <vBC>25.15</vBC>
            <pPIS>0.0000</pPIS>
            <vPIS>0.00</vPIS>
        </PISAliq>
    </PIS>
    <COFINS>
        <COFINSAliq>
            <CST>01</CST>
            <vBC>25.15</vBC>
            <pCOFINS>0.0000</pCOFINS>
            <vCOFINS>0.00</vCOFINS>
        </COFINSAliq>
    </COFINS>
</imposto>

1 answer

0

From what I understand you access the parent node that would be "", but only made a deal for his possible children.

When accessing the parent, you will need to loop it and create a condition for each of the children you want to access and so on.

It’s a somewhat stressful job, in my case I started working directly with String, reading the XML file and working and manipulating the string in XML to achieve the desired results, which saved a lot of development time.

Browser other questions tagged

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