Read complex XML with PHP

1

Good day, I have an XML file which I have difficulty reading, my problem is in the labels, since they are not simple labels, the Teachers start with cfdi and the ones that have internally you start with ecb, which are what I I'm interested in getting its content, I've tried several methods, but I'm not successful, I hope you can help me, I'm new to XML, greetings XML:

<?xml version="1.0" encoding="utf-8"?>
  <cfdi:Comprobante Version="3.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">

    <cfdi:Complemento>
      <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/......." Version="1.1" UUID="123" FechaTimbrado="123" RfcProvCertif="123" SelloCFD="123"  />
    </cfdi:Complemento>

    <cfdi:Addenda>
      <ecb:EstadoDeCuentaBancario xmlns:ecb="http://schemas.reachcore.com/addendas/addenda-ecb/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.reachcore.com/........ http://go.reachcore.com/......." version="1.0">
        <ecb:Movimientos>
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
          <ecb:MovimientoECB fecha="123" referencia="123" descripcion="123" importe="123" moneda="123" saldoAlCorte="123" />
        </ecb:Movimientos>
      </ecb:EstadoDeCuentaBancario>
    </cfdi:Addenda>
  </cfdi:Comprobante>

PHP:

<?php
  $xml = <<<EOD
   -- Texto XML-- 
  EOD;
  $dom = new DOMDocument;
  $dom->loadXML($xml);
  foreach ($dom->getElementsByTagNameNS('*', '*') as $element){
    echo 'local name: ', $element->localName, ', prefix: ', $element->prefix,"\n";
  }
?>

Until now with this code I have been able to get the labels, but not the content of each <ecb:MovimientoECB> greetings and thanks for your time and help

    
asked by Gadiel Navarrete 19.10.2017 в 17:52
source

2 answers

1

Look at this example:

libxml_use_internal_errors(true); 
$xml="";
try{
    $xml = new SimpleXMLElement($strContenidoXML);
}catch(Exception $e){
    //aqui maneja el error
}
$noNodosRet =  count($xml->xpath("/cfdi:Comprobante/cfdi:Impuestos/cfdi:Retenciones/cfdi:Retencion"));//extrae el numero de nodos retencion
$arrayImporteRetV32 =$xml->xpath("/cfdi:Comprobante/cfdi:Impuestos/cfdi:Retenciones/cfdi:Retencion/@importe");//Xpath que saca los importes V32
$arrayImpuestoRetV32=$xml->xpath("/cfdi:Comprobante/cfdi:Impuestos/cfdi:Retenciones/cfdi:Retencion/@impuesto");//Xpath que saca los impuestos V32
for($i=0;$i<$noNodosRet;$i++){
    $valorImporteRet =  "".trim($arrayImporteRetV32[$i]->importe);
    $valorImpuestoRet =  "".trim($arrayImpuestoRetV32[$i]->impuesto);
}
    
answered by 19.10.2017 / 19:26
source
0

Try this:

<?php
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
  <cfdi:Comprobante Version="3.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">

    <cfdi:Complemento>
      <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/......." Version="1.1" UUID="123" FechaTimbrado="123" RfcProvCertif="123" SelloCFD="123"  />
    </cfdi:Complemento>

    <cfdi:Addenda>
      <ecb:EstadoDeCuentaBancario xmlns:ecb="http://schemas.reachcore.com/addendas/addenda-ecb/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.reachcore.com/........ http://go.reachcore.com/......." version="1.0">
        <ecb:Movimientos>
          <ecb:MovimientoECB fecha="fecha1" referencia="referencia1" descripcion="descripcion1" importe="importe1" moneda="moneda1" saldoAlCorte="saldoAlCorte1" />
          <ecb:MovimientoECB fecha="fecha2" referencia="referencia2" descripcion="descripcion2" importe="importe2" moneda="moneda2" saldoAlCorte="saldoAlCorte2" />
          <ecb:MovimientoECB fecha="fecha3" referencia="referencia3" descripcion="descripcion3" importe="importe3" moneda="moneda3" saldoAlCorte="saldoAlCorte3" />
          <ecb:MovimientoECB fecha="fecha4" referencia="referencia4" descripcion="descripcion4" importe="importe4" moneda="moneda4" saldoAlCorte="saldoAlCorte4" />
          <ecb:MovimientoECB fecha="fecha5" referencia="referencia5" descripcion="descripcion5" importe="importe5" moneda="moneda5" saldoAlCorte="saldoAlCorte5" />
          <ecb:MovimientoECB fecha="fecha6" referencia="referencia6" descripcion="descripcion6" importe="importe6" moneda="moneda6" saldoAlCorte="saldoAlCorte6" />
        </ecb:Movimientos>
      </ecb:EstadoDeCuentaBancario>
    </cfdi:Addenda>
  </cfdi:Comprobante>
XML;

$factura = simplexml_load_string($xml);
$namespaces = $factura -> getNamespaces(true);
$factura -> registerXPathNamespace('e', $namespaces['ecb']);

foreach ($factura -> xpath('//e:MovimientoECB') as $m) {
    echo($m['fecha']." <br/>");
    echo($m['referencia']." <br/>");
    echo($m['descripcion']." <br/>");
    echo($m['importe']." <br/>");
    echo($m['moneda']." <br/>");
    echo($m['saldoAlCorte']." <br/>");
}
?>
    
answered by 06.02.2018 в 00:46