0

I have that php parser, its work perfectly but I get with this only first image:

<?php
header("Content-Type: text/html;charset=UTF-8");
$xml = simplexml_load_file("xmlf.xml") 
or die("Error: Cannot create object");
foreach($xml->children() as $cars){

$code = "$cars->code";
$category = "$cars->category_id";
$brand = "$cars->brand";
$name = "$cars->product_name";
$img1 = $cars->image;

echo $code;
echo "^";
echo $name;
echo "^";
echo $descc;
echo "^";
echo $category;
echo "^";
echo $weight;
echo "^";
echo $brand;
echo "^";
echo $img1;
echo "\n";

} ?>

And here sample of xml file what I need to parse:

    <?xml version="1.0" encoding="UTF-8"?>
<xml> 
 <good> 
<code>CODE1</code>
<product_name>NAME</product_name>
<brand></brand>
<category_id>725</category_id> 
<category>CAT</category>
<price>1.42</price>
<quantity>2</quantity>
<image count="1">http://imagelink.jpg</image> 
<image count="2">http://imagelink.jpg</image> 
<image count="3">http://imagelink.jpg</image> 
<image count="4">http://imagelink.jpg</image> 
<image count="5">http://imagelink.jpg</image> 
</good>

 <good> 
<code>CODE2</code>
<product_name>NAME</product_name>
<brand></brand>
<category_id>725</category_id> 
<category>CAT</category>
<price>1.42</price>
<quantity>2</quantity>
<image count="1">http://imagelink.jpg</image> 
<image count="2">http://imagelink.jpg</image> 
</good>

</xml>

Please help me, what i need to change to parse all images in this xml? I spent around six hours found solution, but not luck still.

user2421781
  • 399
  • 2
  • 5
  • 16

1 Answers1

2

if there is more then one <image>:

foreach ($xml->good as $car) {

    echo $car->code . PHP_EOL;
    echo $car->category . "($car->category_id)" . PHP_EOL;
    echo $car->brand . PHP_EOL;

    foreach ($car->image as $image)
        echo "image No." . $image['count'] . ": " . $image . PHP_EOL;

}

see it working: https://eval.in/95876

michi
  • 6,565
  • 4
  • 33
  • 56