简体   繁体   中英

php loadXML string

I have a xsd string and I need to get the contents inside tag status and tags itens/seloRecebimento and place it like a table.

What I need:

$status = 0; // or 1

The tags contents on itens/seloRecebimento like this:

  1. 4 AAA034593 A1B2
  2. 4 AAA034594 A1B2

Below is the xsd string:

$string = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
  <ns4:receberSelosResponse xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://www.tjce.jus.br/selodigital/schemas" xmlns:ns4="http://service.recebimento.selodigital.tjce.jus.br/">
     <return>
        <cabecalho>
           <versao>1.12</versao>
           <dataHora>2019-12-30T08:17:17.425-03:00</dataHora>
           <ambiente>2</ambiente>
           <serventia>
              <codigoServentia>000309</codigoServentia>
           </serventia>
        </cabecalho>
        <itens>
           <itemSolicitacao>
              <sequencial>1</sequencial>
              <status>
                 <codigo>MSG037</codigo>
                 <status>0</status>
                 <mensagem>Item da solicitação já foi entregue para serventia.</mensagem>
              </status>
              <seloRecebimento>
                 <codigoSelo>
                    <codigo>4</codigo>
                 </codigoSelo>
                 <numeroSerie>AAA034593</numeroSerie>
                 <validador>A1B2</validador>
              </seloRecebimento>
              <seloRecebimento>
                 <codigoSelo>
                    <codigo>4</codigo>
                 </codigoSelo>
                 <numeroSerie>AAA034594</numeroSerie>
                 <validador>A1B2</validador>
              </seloRecebimento>                 
           </itemSolicitacao>
        </itens>
     </return>
  </ns4:receberSelosResponse>
</soap:Body>
</soap:Envelope>';

What I did so far:

$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$selos = array();
$selos_r = $domDocument->getElementsByTagName("itens");
foreach($selos_r as $selos_rs){
    foreach($selos_rs->childNodes as $valor)    {
        if($valor instanceof DOMElement)        {
            array_push($selos, $valor->textContent);
        }
    }
}

print_r($selos);

The result:

Array ( [0] => 1 MSG037 0 Item da solicitação já foi entregue para serventia. 4 AAA034593 A1B2 4 AAA034594 A1B2 ) 

At the moment, you are just dumping out all of the DOMElement contents. This code uses XPath to pick out the bits that you want and put them into an array.

Each XPath starts from the current element your working with as the context and use descendant:: to ensure that it fetches the data inside the element. Also using evaluate() as this can return the string value...

$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$xp = new DOMXPath($domDocument);

$selos = array();
$selos_r = $domDocument->getElementsByTagName("itens");
foreach($selos_r as $selos_rs){
    $status = $xp->evaluate('string(descendant::status/status)', $selos_rs);
    foreach($selos_rs->getElementsByTagName("seloRecebimento") as $s)    {
        $codigo = $xp->evaluate('string(descendant::codigo)', $s);
        $numeroSerie = $xp->evaluate('string(descendant::numeroSerie)', $s);
        $validador = $xp->evaluate('string(descendant::validador)', $s);
        $selos[] = [$status, $codigo, $numeroSerie, $validador];
    }
}

print_r($selos);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM