Jump to content
rony2006

Reading data from XML - not found data

Recommended Posts

rony2006

Hello, 

I have the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" version="3.2" serie="AFAD" folio="156" fecha="2017-11-08T14:55:27" sello="Z5UgoQLYj+eAj3LGqwObJp7q+DXazvMC8jgfhgf" formaDePago="Pago en una sola exhibición" noCertificado="00001000000406658711" certificado="MIIGFjCCA/6gAwIBAgIUMDAwMDEwMDAwMDA0MDY2NTg3MTEwDQYJKoZIhvcNAQELBQAwggGyMTgwNgYDVQQDDC9BLkMuIGRlbCBTZXJ2aWNpbyBkZSBBZG1pbmlzdHJhY2nDs24gVHJpYnV0YXJpYTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3RyYWNpw7NuIFRyaWJ1dGFyaWExODA2BgNVBAsML0FkbWluaXN0cmFjacOzbiBkZSBTZWd1cmlkYWQgZGUgbGEgSW5mb3JtYWNpw7NuMR8wHQYJKoZIhvcNAQkBFhBhY29kc0BzYXQuZ29iLm14MSYwJAYDVQQJDB1Bdi4gSGlkYWxnbyA3NywgQ29sLiBHdWVychgfgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAplsw+drENNc3zPFq+FZN0s/0lwmoECUNc27Y4DC4HC2gjbDQR5i+ZfXxsRkiNuKFtHhhatu4htzKOonlbo5/sb99OGG/XhXG2MNGLUgnvmNyWptXaCYKeSSDMlJ3b1/LsaQxu9U2k1crWS4Ut9HJkQF0Eo6Lblnw8l4zGn5ikvFuAeMVcZdQdh0TTBPMuhh0VF7/PyOGFuE50i/51sMVTZRtshwDGMqEqCbhF/SXsUWNMcX6OWIQAHyrAWGZT2HpcGXBVvq0Ljw8CGfdGWFJMQ20z/sdbH/ypP/CSl8bHPS5wo2Uu8aFY+VCZki6kKIU2vxz9hJBXFxW/XK/rZR0BQIDAQABox0wGzAMBgNVHRMBAf8EAjAAMAsGA1UdDwQEAwIGwDANBgkqhkiG9w0BAQsFAAOCAgEAaexWNAFeycqwOSumk8T20Iplv5djMR0JSFn37T0KK+dmlw3aYvCrwZH3/T1kyV2916yhZvnQxaIne3+Wb/k8FWykvPbYM369/QcxZCJjtZ9tCFihztKsWDrBGV/gRtxLtkn5fKgRCo7bMRdwi0SloeAjPO4JppD+/pFDmbt1h8sT/4UpZ7gBE3XQyiwfwySX4ZH2AYMzAEGhwvLFWkXym3T7bd6YY7QMgqrbzYKUQflRtAqxQdnQx6NFXzcBNYqtPhVGJ6CNiL141WFio6C8//FVy1RCQxdKOmGKXYO3f2qZ5R/tgJvydJiSFuUxjMCLY28F140J7xrW08eUgZCvnMvMtMJwz545c/tWLrHQahqOGaszDFgnlAfH4JkZdv+1ma2A1ZP+pT2aSMHyww9qIAN3jBVi1GQdEUDDCbPYQTP7eNBgH5MHs7gTn+Of9YPFYPFxDdHjHUJBVn9BOu10K06uBZAutEW2Bt6Obk1XWhu1oFG3OnEa7SX0Stpn+QWABwgYLxZTzMaPUzQ6PYR4wRGSM7Un2IKbNRLqJ4mRXlsYqPytjsnEn41qfnIJ+ZB2MtCCZbks3S89D2CnvXW5oOnuCCl0C6+sJge71YAJo4hPQ/hSNFVDqZci57/1UZ4MTyLKl4c4lzCIv6ryPEw/fq7Uw/+7zYKsMX80m8y5sdQ=" subTotal="601894.079825" descuento="0.00" TipoCambio="1" Moneda="MXN" total="698197.132597" tipoDeComprobante="ingreso" metodoDePago="03" LugarExpedicion="Deleg. B5r43t4éxico" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd">
  <cfdi:Emisor rfc="DIgfdV4" nombre="DeeFGGGFDC.V.">
    <cfdi:DomicilioFiscal calle="CaREW" noExterior="NBVCVNBV57 A" noInterior="REW" colonia="6546" municipio="543" estado="MNBMNB" pais="8987" codigoPostal="987897" />
    <cfdi:RegimenFiscal Regimen="76e9987" />
  </cfdi:Emisor>
  <cfdi:Receptor rfc="CNC140563677828PQ4" nombre="JHGJHG">
    <cfdi:Domicilio calle="G54" noExterior="32" colonia="JHG" municipio="De7654" estado="YTGHF65" pais="México" codigoPostal="01780" />
  </cfdi:Receptor>
  <cfdi:Conceptos>
    <cfdi:Concepto cantidad="1" noIdentificacion="20171105M004IE1-A12050" unidad="No aplica" descripcion="Devolución del sobrecobro por pérdidas marginales en el MDA a los exportadores" valorUnitario="17035.72" importe="17035.72" />
    <cfdi:Concepto cantidad="1" noIdentificacion="20171105M004IE1-A15050" unidad="No aplica" descripcion="Excedente de la liquidación de los DFT en el MDA a exportadores" valorUnitario="8770.42" importe="8770.42" />
    <cfdi:Concepto cantidad="753" noIdentificacion="20171105M004IE1-B02050" unidad="MWh" descripcion="Compra de la energía para exportación en el MTR" valorUnitario="765.057025" importe="576087.939825" />
  </cfdi:Conceptos>
  <cfdi:Impuestos totalImpuestosTrasladados="96303.052772">
    <cfdi:Traslados>
      <cfdi:Traslado impuesto="IVA" tasa="16.00" importe="96303.052772" />
    </cfdi:Traslados>
  </cfdi:Impuestos>
  <cfdi:Complemento>
    <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" version="1.0" UUID="4CA1B7YTR8B3-A1EA-5CC45629B5AC" FechaTimbrado="2017-11-08T14:55:28" selloCFD="Z5UgoQLYj+eAj3LGqwOYTRFfxrp2mMzU46li/PnhGkXjGteJURuGGcWE6mevjlSGTMtVsDp7KiP654qFEwwQDedchLZaPWXA/aiqA4x1zLADe46fv+GKDcjQAq3AlL5jeWykQAUgQ0sCqcUTI8na+dmq41t3KMoGwWR3jNiqMkCDS64fBkVkd5Po4TxA0XMx+V2uAu0sXItdVZRIrxljsLRmhlkzPZV1dv5o1YL850z3abv6MVkWjCXFtCsFUShbDDWm6OZsltwCtO/ZfIKyqI6Jy/u6pucUUPdB+a1vrSs0EoqLZunBBVHOGL+sq0mPnuHi+O5oOA==" noCertificadoSAT="00001000000403038987" selloSAT="tY6BzU33nmKJHG+6d6J5WTKuvYCp0NaBt4VaC20srZfA876k/dRLHTycKYsZpm3YBFcW/ZTAU+U4GfjCcWLXP8eYG9EkNQRPEsCDHUe+DWplJ2ITBLDJRyCP4xiJK7URPo3png/N1bz3LmKymm+AEGe10PdsUcZZLQVxjJPl6WLckeoDaCrFJGxB8lt9yl0I2gfO/wxDzMBtEnwkKT/4thO89Weau+gSyBtZ0DOEVikCJehe5Tmr1YJUkCULwgXek6mvj7TLqlJaJces+jD0QCRxAvxZYS0q27fTFcTfeTDu1bMcPWXSdI8ZCUfM5ygVd85LQi5BGgNFR87xA==" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/TimbreFiscalDigital/TimbreFiscalDigital.xsd" />
  </cfdi:Complemento>
  <cfdi:Addenda>
    <ad:ADENDAS xmlns:ad="http://www.addenda.com">
      <ad:CAB LABEL="CABADD" TIPO_DOC="1" CODIGO_FUF="20171105M004IE1P00" PERIODO_ECD="2017-11-05" FECHA_LIM_PAGO="2017-11-15" ID_PARTIC_CENACE="M004IE1" BANCO="BBVA Bancomer, S.A." SUCURSAL="70" CUENTA="0104888553" CLABE="012180001048885534" REF_BANCO="20171105M004IE1P00" CONTACTO="ja,MN" IMPORTE_TOTAL="698197.14 " MONTO_LETRA="Seiscientos noventa y ocho mil ciento noventa y siete pesos y catorce centavos">
        <ad:Conceptos>
          <ad:LINEA LABEL="LINADD" NUM_LIN="1" FOLIO_UNICO="20171105M004IE1-A12050" CONCEPTO="Devolución del sobrecobro por pérdidas marginales en el MDA a los exportadores" CANTIDAD="1" UNIDAD="N/A" PRECIO_UNIT="17035.72 " IMPORTE_LINEA="17035.72" IVA="2725.72" TOTAL="19761.44" />
          <ad:LINEA LABEL="LINADD" NUM_LIN="2" FOLIO_UNICO="20171105M004IE1-A15050" CONCEPTO="Excedente de la liquidación de los DFT en el MDA a exportadores" CANTIDAD="1" UNIDAD="N/A" PRECIO_UNIT="8770.42 " IMPORTE_LINEA="8770.42" IVA="1403.27" TOTAL="10173.69" />
          <ad:LINEA LABEL="LINADD" NUM_LIN="3" FOLIO_UNICO="20171105M004IE1-B02050" CONCEPTO="Compra de la energía para exportación en el MTR" CANTIDAD="753" UNIDAD="MWh" PRECIO_UNIT="765.06 " IMPORTE_LINEA="576087.94" IVA="92174.07" TOTAL="668262.01" />
        </ad:Conceptos>
      </ad:CAB>
    </ad:ADENDAS>
  </cfdi:Addenda>
</cfdi:Comprobante>

 

 

I try to get the value for UUID from TimbreFiscalDigital.

 

#include <MsgBoxConstants.au3>


Local $xmlPath = @ScriptDir & '\xmlfile2.xml'
If Not FileExists($xmlPath) Then Exit
$file = FileRead($xmlPath)
$file = StringReplace($file, @LF, '')
$file = StringSplit($file, @CR, 1)



Global $UUID_XML = XMLget($file, 'cfdi:Comprobante\cfdi:Complemento\tfd:TimbreFiscalDigital\UUID')



MsgBox($MB_SYSTEMMODAL, "jhgjhg", $UUID_XML)









Func XMLget($file, $Path); XMLget($file,'adc_database\currentconditions\realfeel')
    $Path = StringSplit($Path, '/\|', 0)
    $lastline = 0
    For $lvl = 1 To $Path[0] Step 1
        For $line = $lastline To $file[0] Step 1
            $lastline = $line
            $hstart = StringInStr($file[$line], '<' & $Path[$lvl] & '>', 0)
            $hstarta = StringInStr($file[$line], '<' & $Path[$lvl] & ' ', 0)
            If $hstart Or $hstarta Then
                If $lvl == $Path[0] Then
                    If $hstart Then
                        $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                        If $end Then
                            $hstart = $hstart + StringLen('<' & $Path[$lvl] & '>')
                            Return StringMid($file[$line], $hstart, $end - $hstart)
                        EndIf
                    EndIf
                    If $hstarta Then
                        $end = StringInStr($file[$line], '/>', 0)
                        If $end Then
                            $hstarta = $hstarta + StringLen('<' & $Path[$lvl] & ' ')
                            $return = StringMid($file[$line], $hstarta, $end - $hstarta)
                            Return $return
                        EndIf
                        $ends = StringInStr($file[$line], '>', 0)
                        If $ends Then
                            $hstart = $ends + 1
                            $end = StringInStr($file[$line], '</' & $Path[$lvl] & '>', 0)
                            If $end Then
                                $return = StringMid($file[$line], $hstart, $end - $hstart)
                                Return $return
                            EndIf
                        EndIf
                    EndIf
                EndIf
                ContinueLoop 2
            EndIf
        Next
        If $line == $file[0] Then ExitLoop
    Next
    Return 'not found'
EndFunc ;==>XMLget

 

But the value is not returned. 

If I use just: 

Global $UUID_XML = XMLget($file, 'cfdi:Comprobante\cfdi:Complemento\tfd:TimbreFiscalDigital\') Then I get a long string with all the data from TimbreFiscalDigital.

I tried to use functions like Global $UUID = StringTrimRight (StringTrimLeft($UUID_XML, 74), 902) ; Remove the 5 leftmost characters from the string. to get the exact data that I want, but the problem is that sometimes other data is longer or shorter and I don't get exactly what I want.

How can I get the exact value for UUID from TimbreFiscalDigital?

 

Any help is appreciated! 

data.JPG

Share this post


Link to post
Share on other sites
orbs

quick snippet that does it:

#include <String.au3>
#include <Array.au3>
Global $sXML = FileRead('data.xml')
Global $aMatches = _StringBetween($sXML, '<cfdi:Complemento>', '</cfdi:Complemento>')
If @error Then MsgBox(0, 'Error', 'container not found.')
Global $sMatch1 = $aMatches[0]
Global $aUUID = _StringBetween($sMatch1, 'UUID="', '"')
If @error Then MsgBox(0, 'Error', 'string not found.')
Global $sUUID = $aUUID[0]
MsgBox(0, 'result', $sUUID)

save the XML data as file "data.xml" and run the code above. you can easily follow the logic, ask if you need any help.

 

Edited by orbs

Share this post


Link to post
Share on other sites
rony2006

Thank you.

 

Looks like is working and I saw what is the logic behind it so I applied it to get also other data.

#include <String.au3>
#include <Array.au3>
Global $sXML = FileRead('xmlfile2.xml')
Global $aMatches = _StringBetween($sXML, '<cfdi:Comprobante', '<cfdi:Emisor')
If @error Then MsgBox(0, 'Error', 'container not found.')
Global $sMatch1 = $aMatches[0]
Global $aUUID = _StringBetween($sMatch1, 'serie="', '"')
If @error Then MsgBox(0, 'Error', 'string not found.')
Global $sUUID = $aUUID[0]
MsgBox(0, 'result', $sUUID)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.