0

Can't figure out how to loop through SimpleXMLElement Attributes. Try different methods, still without success..

here is xml:

<items>
<main>
<user_id>3370</user_id>
<msg_count>19</msg_count>
</main>
<msg category="/real-estate/residences/riga/" internal_id="51092" sid="Pārdod">
<phones>1111111111</phones>
<email>e@email.com</email>
<images>
<imgs>https://www.website.com/uploads/object/51092/img-0404jpg-62079a4938e47.jpg</imgs>
<imgs>https://www.website.com/uploads/object/51092/img-0405jpg-62079a496185a.jpg</imgs>
<imgs>https://www.website.com/uploads/object/51092/img-0406jpg-62079a494b2a5.jpg</imgs>
</images>
<options>
<opt name="title1" currency="€">299000.00</opt>
<opt name="title2">3</opt>
<opt name="title3">6</opt>
<opt name="title4" currency="m2">550</opt>
<opt name="Platība">226</opt>
<opt name="Iela">Pildas</opt>
<opt name="Nr.">30C-32</opt>
<opt name="Mājas tips">Mūra ēka</opt>
</options>
<text>
<![CDATA[ Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima optio repellendus provident vel necessitatibus culpa vero, molestias aliquam ut. Eaque ad eius accusantium ipsa, molestiae similique blanditiis. Doloribus corrupti sed, ullam iste unde totam tempore? Amet et labore accusamus expedita eveniet minus ducimus quam dolorum quidem facilis quae, totam quas? ]]>
</text>
</msg>
<msg category="/red/white/" internal_id="52690" sid="today">
<phones>000000000000</phones>
<email>e@email.com</email>
<images>
<imgs>https://www.website.com/uploads/object/52690/img-1651jpg-627398a6bdc90.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1652jpg-627398a6e12ff.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1663jpg-627398b612a3e.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1658jpg-627398a4da68f.jpg</imgs>
</images>
<options>
<opt name="title1" currency="€">52000.00</opt>
<opt name="title2">5</opt>
<opt name="title3">3</opt>
<opt name="title4">1</opt>
<opt name="title5">31</opt>
<opt name="title6">xxx</opt>
<opt name="title7" currency="km">10</opt>
<opt name="Nr.">16</opt>
<opt name="type">one</opt>
</options>
<text>
<![CDATA[ Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima optio repellendus provident vel necessitatibus culpa vero, molestias aliquam ut. Eaque ad eius accusantium ipsa, molestiae similique blanditiis. Doloribus corrupti sed, ullam iste unde totam tempore? Amet et labore accusamus expedita eveniet minus ducimus quam dolorum quidem facilis quae, totam quas? ]]>
</text>
</msg>
</items>

here is my php:

<?php
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://www.------.com/xml/ss?id=178&user_id=3370",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Content-Type: text/xml"
],
]);

$response = curl_exec($curl);
$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {

foreach($xml->msg as $row){

  echo $row->phones. "<br />";
  echo $row->email. "<br />";
  echo $row->text. "<br />";

  foreach ($row->attributes() as $key => $value) {

    echo $key ." : ". $value ."<br />";
  }

  foreach($row->images->imgs as $key => $value){

      echo "<img src=".$value." alt='' width='150px' height='auto'> ";
      //echo $key . $value;
  }
  echo "<br />";
  
  foreach ($row->options->opt->attributes() as $value) {
    
      echo  $value ." : ";

      foreach($row->options->opt as $key => $value){

        echo  $value. "<br />";

    }
  }
}

}
?>

Here is the part where Im stuck:

enter image description here

Need some help, .. enter image description here This is the part I need to loop through () and get something like this (attributes name: value / title1 : € : 299000.00 ) -->

Maybe is some better way to success, ... ::)) Thanks!

Axīc
  • 21
  • 1
  • 6
  • See first example: https://www.php.net/manual/en/simplexmlelement.attributes.php#refsect1-simplexmlelement.attributes-examples – Sammitch Dec 08 '22 at 23:19
  • You've actually already written the code in lines 4-7. – Sammitch Dec 08 '22 at 23:20
  • Im try this : foreach($xml->msg->options->opt[0]->attributes() as $a => $b) {...} without success .. on line 6 and 8. get only first line and messy – Axīc Dec 08 '22 at 23:28
  • lines 4-7 dublicate records .. and attributes not show up for all . only first one. sorry for english :) – Axīc Dec 08 '22 at 23:50

1 Answers1

0

If that is really the XML you're using as input, there is an excess closing MSG tag. That might cause some troubles with parsing.

<items>
  <main>
    <user_id>3370</user_id>
    <msg_count>19</msg_count>
  </main>
</msg> <!-- THIS ONE -->

And regarding the options, if I understood correctly, you might want to loop through the options just once and inside that to loop through the attributes of the option you're currently in.

  foreach ($row->options->opt as $opt) {
    
      foreach($opt->attributes() as $key => $value){
        echo  $value. ": ";
      }

    echo  $opt->__toString() ."<br>";

  }
Tommander
  • 76
  • 5
  • sorry my fault, there isn't – Axīc Dec 09 '22 at 00:02
  • So if you replace lines 44-53 (incl.) in the PHP code you posted (starting with `foreach ($row->options->opt->attributes() as $value) {`) with what I posted in the answer, does it do what you were expecting? Because if I'm trying it on my local environment, it seems to do what you were mentioning. If not, what is the expect output? – Tommander Dec 09 '22 at 00:12
  • Happy to help @Axīc, please let me know if you have some additional question to that :) – Tommander Dec 09 '22 at 00:15
  • Ok, one more question. What is the best way to separate texts in ... , in my full xml text is in 3 languages separated with \n. something like this eng.... \n ger.....\n fr..... best solution would be echo out all 3 languages separately. :) – Axīc Dec 09 '22 at 00:25
  • There is a nice function for that called [explode](https://www.php.net/manual/en/function.explode). So just `$text = explode("\n", $row->text)` will get you a simple array, where one item = one language. – Tommander Dec 09 '22 at 00:32
  • show warning (Warning: Array to string conversion.. ) – Axīc Dec 09 '22 at 00:40
  • Yeah, forgot to mention that you have to loop through that array to output to screen. So `foreach ($text as $one) { echo $one; }` added under the explode line will help. – Tommander Dec 09 '22 at 00:42