New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
PHP Simple not a object error
Hi
Could someone look at this PHP code and see why the second addchild will generate an error " addChild() on a non-object"?
Thanks
$rtaXML = new SimpleXMLElement("<records></records>");
foreach($vas as $key=>$val){
$rtaBib[$key]=$rtaXML->addChild('bibrecord')->addAttribute('id',$key);
foreach($val as $copy=>$item){
$rtaBib[$key]->addChild('item');
}
}
echo $rtaXML->asXML();
Here is the XML I am trying to build
<record>
<bibrecord>
<item>
something
</item>
</bibrecord>
<bibrecord>
<item>
something
</item>
</bibrecord>
</record>
Comments
The first addChild() is done on the $rtaXML object while the second one is not. The second one is attempting to be done on the $rtaBib array which is not an object.
php.net/manual/en/simplexmlelement.addattribute.php isn't returning anything meaning $rtaBib[$key] is not an object. I'm not sure what are you trying to do as that code doesn't make much sense to me, but you maybe wanted something like this:
$rtaBib[$key]=$rtaXML->addChild('bibrecord'); $rtaBib[$key]->addAttribute('id',$key);
Here is the XML I am trying to build. @vedran I am trying to add additional child and some element could be repeated like multiple bibrecord
Anyone has suggestions?
You will want to output the contents of
$rtaBib[$key]
and$rtaXML
using var_dump(). Either of those is likely to be something different from what you expect it to be.If I want to add a child on a child how to do that?