CodeIgniter Forums

Full Version: xml reading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]dimis[/eluser]
I have this xaml variable at my view
Code:
<?xml version="1.0" encoding="UTF-8" ?>
- <lenses>
- <producer>
  <producername><p>o-2 Descr</p></producername>
- <mylenses>
  <price>80.00</price>
  <name>Hi-Performance 1.5 Full Top</name>
  <descr><p>lighter and safer than glass (full coatings: Anti-Reflective + anti-scratch).</p></descr>
  <id>1</id>
  </mylenses>
- <mylenses>
  <price>12.00</price>
  <name>were</name>
  <descr><p>were</p></descr>
  <id>4</id>
  </mylenses>
  </producer>
- <producer>
  <producername><p>2-4 descr</p></producername>
- <mylenses>
  <price>92.00</price>
  <name>Hi-Performance Trivex 1.53 Full Top</name>
  <descr><p>Hi-Performance Trivex 1.53 Full Top</p></descr>
  <id>2</id>
  </mylenses>
  </producer>
  </lenses>
How I can make a loop for each producer and read the name and the values for each mylenses?

El Forum

[eluser]Dready[/eluser]
Hello,

if you're using PHP5 I strongly suggest you use the simpleXML feature.

It will bring you something like :

Code:
// $xmldata contains what you posted
$xml = simplexml_load_string ( $xmldata );

foreach ( $xml->producer as $producer ) {
   foreach ( $producer->mylenses as $lenses ) {
     echo (string)$lenses->name.' '.(string)$lenses->descr."<BR>";
   }
}

El Forum

[eluser]JoostV[/eluser]
There is a class on phpclasses.org I used some time in the past. One file, does the trick. It converts your XML into a multidimensional array. Easy enough to convert into a CI library file.

Download here