Search code examples
phpxmlsimplexml

Can't get SimpleXMLElement content


Sorry, but I coulnd't find anything that solved my problem.

I'm using PHP and I have the following xml as a string:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <nfeResultMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeInutilizacao4">
      <retInutNFe versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">
        <infInut>
          <tpAmb>2</tpAmb>
          <verAplic>AJXNZiasn</verAplic>
          <cStat>102</cStat>
          <xMotivo>Inutilizacao de numero homologado</xMotivo>
          <cUF>20</cUF>
          <ano>20</ano>
          <CNPJ>169</CNPJ>
          <mod>65</mod>
          <serie>123</serie>
          <nNFIni>140</nNFIni>
          <nNFFin>140</nNFFin>
          <dhRecbto>2020-04-20T15:27:49-03:00</dhRecbto>
          <nProt>9019848505847</nProt>
        </infInut>
      </retInutNFe>
    </nfeResultMsg>
  </soap:Body>
</soap:Envelope>

And I'm using the SimpleXMLElement as follows:

function xmlAdapter(string $xml) {
    $xmlElement = new \SimpleXMLElement($xml);
    $document = [
      'xmlElement'    => $xmlElement,
      'xml'           => $xml
    ];

    return $document; }

But document->xmlElement returns {}.

What I want is to grab everything that is inside nfeResultMsg tag, but I couldn't find a way to access it. I wanna save a new xml document just with the content from nfeResultMsg.

Does anybody knows how I could do something like it? Thx a lot.


Solution

  • Putting aside the function and everything else, in order to just "to grab everything that is inside nfeResultMsg tag" you can use

    $xml->xpath('//*[local-name()="nfeResultMsg"]');