I'm trying to read a big XML file using XMLReader and I can't find a way to loop through a subtree correctly.
So far, I tried to use the read() and the next() functions. And it's not working properly. Here is the XML structure that I'm parsing:
<CLIENTES>
<CLIENTE>
<CODIGO_INTERESSADO>10</CODIGO_INTERESSADO>
<NOME_INTERESSADO>Pedro</NOME_INTERESSADO>
<ENDERECO />
<COMPLEMENTO />
<ESTADO />
<MUNICIPIO />
<BAIRRO />
<CEP />
<DATA_CADASTRO>16/09/2015</DATA_CADASTRO>
<STATUS>Ativo</STATUS>
<TELEFONES>
<TELEFONE>
<NUMERO>(21) 96909-6905</NUMERO>
<TIPO>Celular</TIPO>
</TELEFONE>
</TELEFONES>
</CLIENTE>
<CLIENTE>
<CODIGO_INTERESSADO>11</CODIGO_INTERESSADO>
<NOME_INTERESSADO>Luiz</NOME_INTERESSADO>
<ENDERECO />
<COMPLEMENTO />
<ESTADO />
<MUNICIPIO />
<BAIRRO />
<CEP />
<DATA_CADASTRO>16/09/2015</DATA_CADASTRO>
<STATUS>Ativo</STATUS>
<TELEFONES>
<TELEFONE>
<NUMERO>(21) 96909-6901</NUMERO>
<TIPO>Celular</TIPO>
</TELEFONE>
</TELEFONES>
</CLIENTE>
</CLIENTES>
As you can see, the node TELEFONES, can have multiple TELEFONE nodes. So I need to loop that and get them individually. So far, this is my code:
$xml = new XMLReader();
$xml->open('xml_formatado_stack.xml');
$cont = 0;
$clientes = array();
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'CLIENTES') {
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'CLIENTE') {
while ($xml->read()) {
$telefone = array();
if($xml->nodeType == XMLReader::ELEMENT) {
if($xml->localName == 'CODIGO_INTERESSADO') {
$xml->read();
echo $xml->value."<br>";
$clientes[$cont]['codigo_interessado'] = $xml->value;
}
if($xml->localName == 'NOME_INTERESSADO') {
$xml->read();
$clientes[$cont]['nome_interessado'] = $xml->value;
}
if($xml->localName == 'ENDERECO') {
$xml->read();
$clientes[$cont]['endereco'] = $xml->value;
}
if($xml->localName == 'COMPLEMENTO') {
$xml->read();
$clientes[$cont]['complemento'] = $xml->value;
}
if($xml->localName == 'ESTADO') {
$xml->read();
$clientes[$cont]['estado'] = $xml->value;
}
if($xml->localName == 'MUNICIPIO') {
$xml->read();
$clientes[$cont]['municipio'] = $xml->value;
}
if($xml->localName == 'BAIRRO') {
$xml->read();
$clientes[$cont]['bairro'] = $xml->value;
}
if($xml->localName == 'CEP') {
$xml->read();
$clientes[$cont]['cep'] = $xml->value;
}
if($xml->localName == 'DATA_CADASTRO') {
$xml->read();
$clientes[$cont]['data_cadastro'] = $xml->value;
}
if($xml->localName == 'STATUS') {
$xml->read();
$clientes[$cont]['status'] = $xml->value;
}
if ($xml->localName == 'TELEFONES') {
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'TELEFONE') {
while ($xml->read()) {
if($xml->nodeType == XMLReader::ELEMENT) {
if($xml->localName == 'NUMERO') {
$xml->read();
$telefone['numero'] = $xml->value;
}
if($xml->localName == 'TIPO') {
$xml->read();
$telefone['tipo'] = $xml->value;
}
}
}
}
}
$clientes[$cont]['telefones'][] = $telefone;
$cont++;
}
}
}
}
}
}
}
var_dump($clientes);
$xml->close();
I'm getting two problems here. First, my final array is having information about only one CLIENTE node. It should have all the CLIENTE nodes, I'm indexing them with the $cont var.
The other problem is that, the TELEFONES node that is going to my $clientes array belongs to the last CLIENTE node of the XML. So, somehow my code is going through every CLIENTE node, but when I treat the TELEFONES node, my $clientes array is getting all messed up.
I just can't find a way to loop a subtree using XMLParser. Can someone help me?