I need to access the value of the numeroCNS
, dataAtribuicao
, etc
tags of the following XML file:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<cad:responseConsultar xmlns:cad="http://servicos.saude.gov.br/cadsus/v5r0/cadsusservice">
<usu:UsuarioSUS xmlns:usu="http://servicos.saude.gov.br/schema/cadsus/v5r0/usuariosus" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<usu:Cartoes>
<usu:CNS>
<cns:numeroCNS xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns">foo</cns:numeroCNS>
<cns:dataAtribuicao xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns">bar</cns:dataAtribuicao>
<cns:tipoCartao xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns">fubá</cns:tipoCartao>
<cns:manual xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns">canjica</cns:manual>
<cns:justificativaManual xmlns:cns="http://servicos.saude.gov.br/schema/cadsus/v5r0/cns" />
</usu:CNS>
</usu:Cartoes>
</usu:UsuarioSUS>
</cad:responseConsultar>
</soap:Body>
</soap:Envelope>
My code:
<?php
$x = simplexml_load_file('xml/teste.xml');
echo $x -> Body -> responseConsultar -> UsuarioSUS -> Cartoes -> CNS ->
numeroCNS;
?>
PHP Notice: Trying to get property of non-object
If you manually remove namespaces , the code works. But it would be impractical to do this in all XML files. What am I doing wrong?