Reading XML with TAG, PHP

Asked

Viewed 122 times

-2

I need to read the XML below. How I access the information?

<?php 
$arq = '<?xml version="1.0" standalone="yes"?>
<ans:mensagemTISS xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ans.gov.br/padroes/tiss/schemas http://www.ans.gov.br/padroes/tiss/schemas/tissV3_02_00.xsd">
<ans:cabecalho>
<ans:identificacaoTransacao>
<ans:tipoTransacao>ENVIO_LOTE_GUIAS</ans:tipoTransacao>
<ans:dataRegistroTransacao>2014-12-22</ans:dataRegistroTransacao>
<ans:horaRegistroTransacao>11:15:01</ans:horaRegistroTransacao>
</ans:identificacaoTransacao>';       

        $sxe = new SimpleXMLElement($arq);

        $namespaces = $sxe->getNamespaces(true);
        var_dump($namespaces);
?>

I have this error return:

Warning: SimpleXMLElement::__construct(): Entity: line 8: parser error : Premature end of data in tag cabecalho line 3 in C:\xampp\htdocs\test\index.php on line 12

Warning: SimpleXMLElement::__construct(): </ans:identificacaoTransacao> in C:\xampp\htdocs\test\index.php on line 12

Warning: SimpleXMLElement::__construct(): ^ in C:\xampp\htdocs\test\index.php on line 12

Warning: SimpleXMLElement::__construct(): Entity: line 8: parser error : Premature end of data in tag mensagemTISS line 2 in C:\xampp\htdocs\test\index.php on line 12

Warning: SimpleXMLElement::__construct(): </ans:identificacaoTransacao> in C:\xampp\htdocs\test\index.php on line 12

Warning: SimpleXMLElement::__construct(): ^ in C:\xampp\htdocs\test\index.php on line 12

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\test\index.php:12 Stack trace: #0 C:\xampp\htdocs\test\index.php(12): SimpleXMLElement->__construct('<?xml version="...') #1 {main} thrown in C:\xampp\htdocs\test\index.php on line 12

2 answers

1

In XML all tags must be closed. In your case, failed to close tags <ans:cabecalho> and <ans:mensagemTISS>, therefore the mistake.

It should be something like this:

$arq = '<?xml version="1.0" standalone="yes"?>
<ans:mensagemTISS xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ans.gov.br/padroes/tiss/schemas http://www.ans.gov.br/padroes/tiss/schemas/tissV3_02_00.xsd">
<ans:cabecalho>
<ans:identificacaoTransacao>
<ans:tipoTransacao>ENVIO_LOTE_GUIAS</ans:tipoTransacao>
<ans:dataRegistroTransacao>2014-12-22</ans:dataRegistroTransacao>
<ans:horaRegistroTransacao>11:15:01</ans:horaRegistroTransacao>
</ans:identificacaoTransacao>
</ans:cabecalho>
</ans:mensagemTISS>';

And to access the data use SimpleXMLElement():

$xmldata = new SimpleXMLElement($arq);
$ns = $xmldata->getNamespaces(false);

foreach($xmldata->children($ns['ans']) as $dados){ 
   echo $dados->identificacaoTransacao->tipoTransacao.'<br>'
   .$dados->identificacaoTransacao->dataRegistroTransacao.'<br>'
   .$dados->identificacaoTransacao->horaRegistroTransacao.'<br>';
}

Reference:

1

Good morning, David.

You can access the information like this:

<?php 
        $arq = '<?xml version="1.0" standalone="yes"?>
                <ans:mensagemTISS xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ans.gov.br/padroes/tiss/schemas http://www.ans.gov.br/padroes/tiss/schemas/tissV3_02_00.xsd">
                    <ans:cabecalho>
                        <ans:identificacaoTransacao>
                            <ans:tipoTransacao>ENVIO_LOTE_GUIAS</ans:tipoTransacao>
                            <ans:dataRegistroTransacao>2014-12-22</ans:dataRegistroTransacao>
                            <ans:horaRegistroTransacao>11:15:01</ans:horaRegistroTransacao>
                        </ans:identificacaoTransacao>
                    </ans:cabecalho>
                </ans:mensagemTISS>';       

        $sxe = new SimpleXMLElement($arq);

        $namespaces = $sxe->getNamespaces(true);

        foreach($sxe->children($namespaces['ans']) as $cabecalho)
        { 
            echo $cabecalho->identificacaoTransacao->tipoTransacao.'<br>';
            echo $cabecalho->identificacaoTransacao->dataRegistroTransacao.'<br>';
            echo $cabecalho->identificacaoTransacao->horaRegistroTransacao.'<br>';
        }
?>
  • 1

    Thank you, you gave it right. This is what I needed. Thank you

Browser other questions tagged

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