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?

I shorted the code a bit and handled the attributes in a differant way. still doesn't solve the error. :(

/*** create a SimpleXML object ***/ 
    if( ! $xml = simplexml_load_file('$file'))
	
	{ 		
		echo "Unable to load XML file";		
	} 

    else
	 
    { 
		
    /*** loop through parent categories ***/ 
	
	$i = 0;
	
	foreach($xml as $node) {
				
		$parentcategory_id  = $xml->parentcategory[$i]->attributes()->id;
		$parentcategory_name = $xml->parentcategory[$i]->attributes()->name;
		 
		 echo "- ".$parentcategory_id." - ".$parentcategory_name."<br />";
	
		 $y = 0;
		 
		 
		 /*** loop through child categories ***/
		  
			 foreach($xml->parentcategory[$i] as $xml->parentcategory[$i]->childcategory[$y]) { 
				 
				
				 $childcategory_id 	=  $xml->parentcategory[$i]->childcategory[$y]->attributes()->id;  
				 $childcategory_name = $xml->parentcategory[$i]->childcategory[$y];
				 
				 echo "--- ".$childcategory_id." - ".$childcategory_name."<br />";
		
				 $y++;
			  
			 }
		  
		$i++;
		
	}
		

}

I finally got it! woohoe!

instead of

foreach($xml as $node) {

it had to be

foreach($xml->parentcategory as $node) {

seems logic when you know ;)

This rocks - thanks for figuring it out. This same issue had me stumped for a few hours now and was driving me nuts... makes sense though when you look at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.