Options

How to correctly read XML to import the data into a table?

isabtogumonisabtogumon Member Posts: 45
Hi,

Currently I have a procedure that reads and imports some elements from the XML to a table correctly, I only need to read some elements well, since some fields are repeated when they are imported.

I get the XML of a response from a WS, the response is stored in InStream and then I read it to import the information into a table, this is part of the code.
if Client.Send(RequestMessage, ResponseMessage) then begin
            ResponseMessage.Content().ReadAs(InStrmResponse);
            Data.SetCurrentKey(TempLineNo);
            if Data.Find('+') then
                TempLineNo := Data.TempLineNo;
            if XmlDocument.ReadFrom(InStrmResponse, xmlDoc) then begin
                xmlnsMgr.AddNamespace('ns', 'http://www.company.com.br');
                if xmlDoc.SelectNodes('//ns:Movimentacao', xmlnsMgr, xmlNodList) then begin
                    foreach xmlNodRoot in xmlNodList do begin
                        xmlNodRoot.SelectSingleNode('ns:Codigo', xmlnsMgr, xmlNod1);
                        xmlNodRoot.SelectSingleNode('ns:TipoMovimentacao', xmlnsMgr, xmlNod2);
                        xmlNodRoot.SelectSingleNode('ns:DataMovimentacao', xmlnsMgr, xmlNod3);
                        xmlNodRoot.SelectSingleNode('ns:EmpresaOrigem/ns:IDUnihealth', xmlnsMgr, xmlNod4);
                        xmlNodRoot.SelectSingleNode('ns:CentroCustoOrigem/ns:IDUnihealth', xmlnsMgr, xmlNod5);
                        xmlNodRoot.SelectSingleNode('//ns:CentroCustoOrigem/ns:CodigoExterno', xmlnsMgr, xmlNod6);
                        xmlNodRoot.SelectSingleNode('ns:EmpresaDestino/ns:IDUnihealth', xmlnsMgr, xmlNod7);
                        xmlNodRoot.SelectSingleNode('ns:CentroCustoDestino/ns:IDUnihealth', xmlnsMgr, xmlNod8);
                        xmlNodRoot.SelectSingleNode('//ns:CentroCustoDestino/ns:CodigoExterno', xmlnsMgr, xmlNod9);
                        xmlNodRoot.SelectSingleNode('ns:TipoDocumento', xmlnsMgr, xmlNod10);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoDocumento', xmlnsMgr, xmlNod11);
                        xmlNodRoot.SelectSingleNode('ns:Produto/ns:IDUnihealth', xmlnsMgr, xmlNod12);
                        xmlNodRoot.SelectSingleNode('ns:Produto/ns:CodigoExterno', xmlnsMgr, xmlNod13);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoProdutoSecundario', xmlnsMgr, xmlNod14);
                        xmlNodRoot.SelectSingleNode('//ns:CodigoEmbalagemProduto', xmlnsMgr, xmlNod15);
                        xmlNodRoot.SelectSingleNode('ns:Quantidade', xmlnsMgr, xmlNod16);
                        xmlNodRoot.SelectSingleNode('ns:Valor', xmlnsMgr, xmlNod17);
                        xmlNodRoot.SelectSingleNode('ns:Lote', xmlnsMgr, xmlNod18);
                        xmlNodRoot.SelectSingleNode('ns:Validade', xmlnsMgr, xmlNod19);
                        xmlNodRoot.SelectSingleNode('ns:CodigoItemDocumento', xmlnsMgr, xmlNod20);
                        xmlNodRoot.SelectSingleNode('ns:Usuario', xmlnsMgr, xmlNod21);
                        xmlNodRoot.SelectSingleNode('ns:CodigoEtiqueta', xmlnsMgr, xmlNod22);
                        xmlNodRoot.SelectSingleNode('ns:Identificador', xmlnsMgr, xmlNod23);

                        TempLineNo := TempLineNo + 1;
                        Data.TempLineNo := TempLineNo;
                        Data.Init();
                        Data.Insert(true);

                        Data.Codigo := (xmlNod1.AsXmlElement().InnerXml);
                        Data.TipoMovimentacao := (xmlNod2.AsXmlElement().InnerText);

                        txtDate := (xmlNod3.AsXmlElement().InnerText);
                        txtNewDate := CopyStr(txtDate, 1, 4)
                        + '-' + CopyStr(txtDate, 6, 2)
                        + '-' + CopyStr(txtDate, 9, 2)
                        + ' ' + CopyStr(txtDate, 12, 2)
                        + ':' + CopyStr(txtDate, 15, 2)
                        + ':' + CopyStr(txtDate, 18, 2);
                        Evaluate(formatDate, txtNewDate);
                        Data.DateMovimentacao := formatDate;

                        Data.EmpresaOrigemIDUnihealth := (xmlNod4.AsXmlElement().InnerText);
                        Data.CentroCustoOrigemIDUnihealth := (xmlNod5.AsXmlElement().InnerText);
                        Data.CentroCustoOrigemCodExterno := (xmlNod6.AsXmlElement().InnerText);
                        Data.EmpresaDestinoIDUnihealth := (xmlNod7.AsXmlElement().InnerText);
                        Data.CentroCustDestinoIDUnihealth := (xmlNod8.AsXmlElement().InnerText);
                        Data.CentroCustDestinoIDCodExterno := (xmlNod9.AsXmlElement().InnerText);
                        Data.TipoDocumento := (xmlNod10.AsXmlElement().InnerText);
                        Data.CodigoDocumento := (xmlNod11.AsXmlElement().InnerText);
                        Data.ProductoIDUnihealth := (xmlNod12.AsXmlElement().InnerText);
                        Data.ProductoCodigoExterno := (xmlNod13.AsXmlElement().InnerText);
                        Data.CodigoProdutoSecundario := (xmlNod14.AsXmlElement().InnerText);
                        Data.CodigoEmbalagemProduto := (xmlNod15.AsXmlElement().InnerText);
                        Data.Quantidade := (xmlNod16.AsXmlElement().InnerText);
                        Data.Valor := (xmlNod17.AsXmlElement().InnerText);
                        Data.Lote := (xmlNod18.AsXmlElement().InnerText);
                        Data.Validade := (xmlNod19.AsXmlElement().InnerText);
                        Data.CodigoItemDocumento := (xmlNod20.AsXmlElement().InnerText);
                        Data.Usuario := (xmlNod21.AsXmlElement().InnerText);
                        Data.CodigoEtiqueta := (xmlNod22.AsXmlElement().InnerText);
                        Data.Identificador := (xmlNod23.AsXmlElement().InnerText);
                        Data.Modify(true);
                    end;
                end;
            end;

So that I can read some elements I had to add a double slash (//) since if I didn't I would see an error similar to this: "The XPath query 'ns:Lote' does not match any nodes", with the code above I I'm not getting that error and the records import apparently fine, but repeated. What should I do so that the records are not repeated in cases that must start with a double bar or what should I do to avoid putting them?

This is the structure of the XML

vcqub62j49bt.jpg
Sign In or Register to comment.