Hi there, i am struggling with my code to read xml with php.
it is giving the following error:
Fatal error: Call to a member function attributes() on a non-object on line red. The weird part of it is that the output is good/as it should be.
if( ! $xml = simplexml_load_file('$file'))
{
echo "Unable to load XML file";
}
else
{
/*** loop through parent categories ***/
$i = 0;
foreach($xml as $node) {
$b = $xml->parentcategory[$i]->attributes();
$parentcategory_id = $b[0];
$parentcategory_name = $b[1];
echo "- ".$parentcategory_id." - ".$parentcategory_name."<br />";
$y = 0;
/*** loop through child categories ***/
foreach($xml->parentcategory[$i] as $xml->parentcategory[$i]->childcategory[$y]) {
$a = $xml->parentcategory[$i]->childcategory[$y]->attributes();
$childcategory_id = $a[0];
$childcategory_name = $xml->parentcategory[$i]->childcategory[$y];
echo "--- ".$childcategory_id." - ".$childcategory_name."<br />";
$y++;
}
$i++;
}
}
Here is a part of the xml file i am reading:
<?xml version="1.0" encoding="UTF-8" ?>
<categories>
<parentcategory id="15" name="about" />
<parentcategory id="6" name="shop">
<childcategory id="9">shoes</childcategory>
<childcategory id="12">hats</childcategory>
<childcategory id="14">belts</childcategory>
</parentcategory>
<parentcategory id="1" name="Uncategorized" />
</categories>
Any clue?