1

I've been looking at the other questions posted here on this subject but they all seem to have in common a "symetric" xml file.

I start by calling:

$xml_testimonials=simplexml_load_file("bck/testimonials.xml");

I cant iterate this file:

<?xml version="1.0" encoding="utf-8"?>

<testimonials>

<description><![CDATA[
<p>Give us your feeback!</p>
 ]]></description>

<testimonials_collection>

<testimonial>
<testimonial_name>
Dummy Name
</testimonial_name>
<testimonial_content>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec libero venenatis, posuere massa     vitae, volutpat massa. Maecenas placerat ac metus ut pulvinar.
</testimonial_content>
</testimonial>

<testimonial>
<testimonial_name>
Dummy Name
</testimonial_name>
<testimonial_content>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec libero venenatis, posuere massa     vitae, volutpat massa. Maecenas placerat ac metus ut pulvinar.
</testimonial_content>
</testimonial> 

<testimonial>
<testimonial_name>
 Dummy Name
</testimonial_name>
<testimonial_content>
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec libero venenatis, posuere massa vitae, volutpat massa. Maecenas placerat ac metus ut pulvinar.
 </testimonial_content>
 </testimonial>

 </testimonials_collection>

 </testimonials>

Im trying to use:

foreach($xml_testimonials->testimonials->testimonials_collection as $testimonial) {
    print $testimonial->testimonial->testimonial_name;
}

and Im getting

Warning: Invalid argument supplied for foreach()

Also is there anyway that I can avoid using and retain the html tags?

Marvin
  • 503
  • 2
  • 7
  • 17

1 Answers1

1

Your XML does load and parse correctly, however when using SimpleXML, the XML document's root node is not represented in the resultant object structure. That means instead of beginning your traversal with <testimonials> (the root node here) the highest level to access is actually <testimonials_collection>.

So your loop should actually look like:

// Iterate <testimonial> nodes beneath <testimonials_collection>
foreach($xml_testimonials->testimonials_collection->testimonial as $testimonial) {
    // And get internal details of each <testimonial> node...
    echo $testimonial->testimonial_name;
}

Here is a demonstration, retrieving Dummy Name from 3 nodes: http://codepad.viper-7.com/rQZwOJ

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Thank you, besides the correct answer you explanation is very clear and with an example. Regarding the CDATA tag is there any chance of not having it? – Marvin Nov 29 '14 at 02:59
  • 1
    @Marvin You're welcome, and I hope you always receive good explanations along with answers people provide you here. Ask for them if you don't get them, because you deserve them as do future readers of the site. – Michael Berkowski Nov 29 '14 at 03:00
  • Hi thank you for getting back to me on this point, I meant if CDATA must always be used if I want to use html tags inside my xml. – Marvin Jan 27 '15 at 03:15
  • @Marvin The HTML tags could be entity encoded as `<tagname>` but it is much cleaner to do it as CDATA. – Michael Berkowski Jan 27 '15 at 03:19
  • It was for a simple backoffice and the CDATA confuses less tecnically inclined people a bit. Even so it is much better than encoding the entities. – Marvin Jan 27 '15 at 03:24