0

Need help with updating some simplexml code I did along time ago. The XML file I'm parsing from is formatted in a new way, but I can't figure out how to navigate it.

Example of old XML format:

<?xml version="1.0" encoding="UTF-8"?>
<pf version="1.0">
 <pinfo>
  <pid><![CDATA[test1 pid]]></pid>
  <picture><![CDATA[http://test1.image]]></picture>
 </pinfo>
 <pinfo>
    <pid><![CDATA[test2 pid]]></pid>
    <picture><![CDATA[http://test2.image]]></picture>
 </pinfo>
</pf>

and then the new XML format (note "category name" added):

<?xml version="1.0" encoding="UTF-8"?>
<pf version="1.2">
 <category name="Cname1">
  <pinfo>
   <pid><![CDATA[test1 pid]]></pid>
   <picture><![CDATA[http://test1.image]]></picture>
  </pinfo>
 </category>
 <category name="Cname2">
  <pinfo>
   <pid><![CDATA[test2 pid]]></pid>
   <picture><![CDATA[http://test2.image]]></picture>
  </pinfo>
 </category>    
</pf>

And below the old code for parsing that doesn't work since the addition of "category name" in the XML:

$pinfo = new SimpleXMLElement($_SERVER['DOCUMENT_ROOT'].'/xml/file.xml', null, true);
foreach($pinfo as $resource) 
 {
  $Profile_id = $resource->pid;
  $Image_url = $resource->picture;

  // and then some echo´ing of the collected data inside the loop
 }

What do I need to add or do completely different? I tried with xpath,children and sorting by attributes but no luck - SimpleXML has always been a mystery to me :)

2 Answers2

0

The category elements exist as their own array when you load the XML file. The XML you are used to parsing is contained within. All you need to do is wrap your current code with another foreach. Other than that there isn't much to change.

foreach($pinfo as $category)
{
    foreach($category as $resource) 
    {
        $Profile_id = $resource->pid;
        $Image_url = $resource->picture;
        // and then some echo´ing of the collected data inside the loop
    }
}
Crackertastic
  • 4,958
  • 2
  • 30
  • 37
0

You were iterating over all <pinfo> elements located in the root element previously:

foreach ($pinfo as $resource) 

Now all <pinfo> elements have moved from the root element into the <category> elements. You now need to query those elements first:

foreach ($pinfo->xpath('/*/category/pinfo') as $resource) 

The now wrong named variable $pinfo is standing a bit in the way so it better do some more changes:

$xml    = new SimpleXMLElement($_SERVER['DOCUMENT_ROOT'].'/xml/file.xml', null, true);
$pinfos = $xml->xpath('/*/category/pinfo');

foreach ($pinfos as $pinfo) {
    $Profile_id = $pinfo->pid;
    $Image_url  = $pinfo->picture;
    // ... and then some echo´ing of the collected data inside the loop
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • this seems like a good way to go, would using xpath enable me to select a specific category instead of all of them ? – voldsomenterprise Oct 10 '14 at 15:07
  • Yes, you can do that as well. See [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](http://stackoverflow.com/q/992450/367456) – hakre Oct 10 '14 at 15:13