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.

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>

Designers: www.linkun.info

Comments

  • vapornodevapornode Member, Provider
    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.

    VaporNode | Tampa Florida VPS, Resource Bundles, Dedicated Servers, Colocation | Proactive Management, DDoS Protection
  • 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>
    

    Designers: www.linkun.info

  • Anyone has suggestions?

    Designers: www.linkun.info

  • joepie91joepie91 Member, 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?

    Designers: www.linkun.info

Sign In or Register to comment.