CodeIgniter Forums
xml reading - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: xml reading (/showthread.php?tid=12218)



xml reading - El Forum - 10-10-2008

[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?


xml reading - El Forum - 10-10-2008

[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>";
   }
}



xml reading - El Forum - 10-10-2008

[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