I can not import all xml subfields with PHP

0

I have a function to import the xml file:

$xml = new SimpleXMLElement($arquivoxml);
foreach($xml->Imoveis->Imovel AS $itens) {
  $ref=$itens->Id;
  $atributospai = $itens->Atributos;
  foreach ($atributospai->children() as $atr) {
    $atrid = $atr->Id;
    $idsatr.="('".$ref."','".$atrid."'),";
  }
  $idsatr=substr($idsatr,0,-1);
  $sql="INSERT INTO tabelaimovel (ref) VALUES ({$ref})"
  $sqlatr="INSERT INTO tabelaatributo (ref, atributo) VALUES ({$idsatr})"
  $conn->query($sql);
  $conn->query($sqlatr);
}

But the problem is that I can not get the corresponding attributes, it always takes only the attribute of the first reference, xml is like this:

<Imoveis>
  <Imovel>
    <Id>4181</Id>
    <Atributos>
      <Atributo>
        <Id>23</Id>
      </Atributo>
      <Atributo>
        <Id>42</Id>
      </Atributo>
    </Atributos>
  </Imovel>
  <Imovel>
    <Id>9985</Id>
    <Atributos>
      <Atributo>
        <Id>40</Id>
      </Atributo>
      <Atributo>
        <Id>53</Id>
      </Atributo>
    </Atributos>
  </Imovel>
</Imoveis>
    
asked by anonymous 13.11.2018 / 21:34

0 answers