Domdocument search XML nested tag

Asked

Viewed 27 times

0

I am searching the XML data below to save in PHP variables for future processing. The XML excerpt below is what I am working on:

inserir a descrição da imagem aqui

Inside the "notas_fiscais", I was able to extract the data correctly until the tag "name". When I get to the "client" I cannot extract correctly, the data is incoherent. I am using the method below:

<?php

error_reporting(0);

//COMO É XML (COM A CHAVE) ABRE DIRETO COMO DOMDocument
$filename = "http://localhost/arq.xml";

$doc = new DOMDocument();
    $doc->load( $filename);
    ## lets read the NF block (NIVEL 1)
    $records = $doc->getElementsByTagName( "nota_fiscal" );
    foreach( $records as $record ) { // NIVEL NF
        ## parse the ID
        $id = $record->getElementsByTagName( "id" );
        $id = $id->item(0)->nodeValue;
        ## parse the TIPO
        $tipo = $record->getElementsByTagName( "tipo" );
        $tipo = $tipo->item(0)->nodeValue;
        ## parse the SERIE
        $serie = $record->getElementsByTagName( "serie" );
        $serie = $serie->item(0)->nodeValue;
        ## parse NUMERO
        $numero = $record->getElementsByTagname( "numero" );
        $numero = $numero->item(0)->nodeValue;
        ## parse DATA_EMISSAO
        $data_emissao = $record->getElementsByTagname( "data_emissao" );
        $data_emissao = $data_emissao->item(0)->nodeValue;
        ## parse NOME
        $nome = $record->getElementsByTagname( "nome" );
        $nome = $nome->item(0)->nodeValue;


           ## lets read the CLIENTE block (NIVEL 2)
           $records_2 = $doc->getElementsByTagName( "cliente" );
           foreach( $records_2 as $record_2 ) { // NIVEL CLIENTE
                ## parse the NOME
                $nome_2 = $record_2->getElementsByTagName( "nome" );
                $nome_2 = $nome_2->item(0)->nodeValue;         
                ## parse the TIPO_PESSOA
                $tp_pessoa_2 = $record_2->getElementsByTagName( "tipo_pessoa" );
                $tp_pessoa_2 = $tp_pessoa_2->item(0)->nodeValue;    
                ## parse the CPF_CNPJ
                $cpf_cnpj_2 = $record_2->getElementsByTagName( "cpf_cnpj" );
                $cpf_cnpj_2 = $cpf_cnpj_2->item(0)->nodeValue;

I did opening a second loop inside the first, but I believe it is not the right way. How could I proceed?

EDIT: If I do the form below, it brings all values below the tag "client", but I can’t separate:

$nome_2 = $record->getElementsByTagname( "cliente" );       
$nome_2 = $nome_2->item(0)->nodeValue; 

inserir a descrição da imagem aqui

1 answer

1


You can use the Domxpath.

To get customer data would be like this:

foreach( $records as $record ) { // NIVEL NF
        ## parse the ID
        $id = $record->getElementsByTagName( "id" );
        $id = $id->item(0)->nodeValue;
        //código emitido

        //Faria assim pra pegar os dados do cliente
        $xpathCliente = new DOMXpath($doc);
        $nome = $xpathCliente->query('cliente/nome', $record)->item(0)->nodeValue;
        $tipoPessoa = $xpathCliente->query('cliente/tipo_pessoa', $record)->item(0)->nodeValue;
        //E assim por diante
}

More about Xpath syntax here.

An online tester of Xpath here.

  • thanks for the reply, but in this case do not answer me, because I need to get all information beyond the name. And XML is mounting this way, I can’t change how to extract the data...

  • It wouldn’t just add the call to the other properties: client[name], client[person type] and so on?

  • What do you mean? can exemplify?

  • something like this ? $name_2 = $record->getelementsbytagname( "client[name]" ); $name_2 = $name_2->item(0)->nodeValue;

  • I edited the main answer to see if it’s clearer.

  • I managed to tidy up, I will accept your answer, only edit I changed in the query: $xpathPesquisa->query('client/name', $record)->item(0)->nodeValue;

Show 1 more comment

Browser other questions tagged

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