I cannot import all xml sublevels with PHP

Asked

Viewed 40 times

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’t get the corresponding attributes, it always only takes the attribute of the first reference, the 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>
  • as you are using two loops, you will sweep only two levels. The ideal would be to create a recursive function that sweeps as long as there are child nodes, you understand?

  • more or less, it would be something like creating a function outside the foreach and calling instead of doing a foreach inside the other?

  • I’ll make a code here for your case. Actually not even need recursion, see that you managed to loop in "Immovel->Immovel", within the scope of the Immovel has another loop "Attributes->Attribute", but I did not understand if this XML is a real example or just a structure demo... Can you explain to me?

  • is just a demo of the structure, it has other fields more

  • Would it be possible to put a demo with the structure used? With false data, of course. Because I need to see which complexity is addressed. The way it is there, the name of the tag (<Attribute>) would be the name of the attribute and the content would be the value, correct? Is that so in 100% of cases? That’s why I ask the example. I’ll save this question here in the favorites and later I’ll answer you.

  • That, the content is the value, I need only that id, it is so in 100% of cases. the whole xml has many fields (a 50), but follows all the same level id pattern, only the attribute and photo that is equal, has 2 more levels (attribute and id)

  • 1

    I found a gambiarra solution.. rs, after running the query, I "cleaned" the clothesline: $idsatr='; then it worked.

  • Leandro, would not be a gambiarra, cleaning variables in loop is a normal practice. Anyway, your script does not scan in discovery process, it only works specifically for the attribute "Id" present in all attributes of the property. In that case, any field other than that won’t pass, you understand? And the main problem I see in this structure is that it doesn’t match SQL, maybe Nosql. Anyway, if you have a guarantee that all XML’s will follow this pattern we can say that the problem is solved, right?

Show 3 more comments
No answers

Browser other questions tagged

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