Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


PHP Simple not a object error
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.

PHP Simple not a object error

dnwkdnwk Member
edited December 2013 in Help

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

  • vapornodevapornode Member
    edited December 2013

    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);

  • dnwkdnwk Member
    edited December 2013

    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

     <record>
      <bibrecord>
        <item>
        something
         </item>
        </bibrecord>
      <bibrecord>
         <item>
          something
         </item>
       </bibrecord>
      </record>
    
  • Anyone has suggestions?

  • joepie91joepie91 Member, Patron Provider

    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.

  • @joepie91 said:
    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?

Sign In or Register to comment.