0

Hi i have an xml data like this

<Product>
<ProductID>13078</ProductID>
<image1>
image_url
</image1>
<image2>
image_url
</image2>
<image3>
image_url
</image3>
</Product>

i want to access images please help!!!

Volkan
  • 51
  • 10
  • 6
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – ThW Dec 29 '15 at 13:42

2 Answers2

1

You can do it using SimpleXMLElement

$xml = simplexml_load_string($xmlString, "SimpleXMLElement");
$json = json_encode($xml);
$array = json_decode($json,TRUE);
unset($array['ProductID']); // You don't want 'ProductID' then You can remove it using unset().
$i=1;
foreach($array as $key=>$val){
    echo $array['image'.$i]; // Here you can get all images in one echo
    $i++;
}
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
0
if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    print_r($xml);
}

you will get a result array

or else if you have XML string use this function

$xml = simplexml_load_string($string);

print_r($xml);

hope this will help you

Your Solution:

    $string = '<Product>
<ProductID>13078</ProductID>
<image1>
image_url
</image1>
<image2>
image_url
</image2>
<image3>
image_url
</image3>
</Product>';

$xml = simplexml_load_string($string);

echo $xml->image1;
Prabu Guna
  • 344
  • 1
  • 3
  • 14