Jump to content

SimpleXML addChild to an element with a specific attribute?


stuartmarsh

Recommended Posts

I'm messing with using XML to create a menu system and I am using SimpleXML.

I'm want to merge several XML files together to produce the final menu XML.

 

The menu XML looks like this:

<menu>
   <item title="Home" link="home.php" />
   <item title="Content" link="content.php" />
</menu>

 

I have another XML called items that looks like this:

<menu>
   <item title="Page1" link="page.php?id=1" addTo="Content" />
   <item title="Page2" link="page.php?id=2" addTo="Content" />
</menu>

I want to loop through the "items" XML and add a new child under the element that has a title that matches 'addTo'.

Is there a quick way of doing this without looping through the menu XML for each item?  Something like $xmlMenu->item->"title="content"->addChild($name, $value)?

 

The final output should resemble this:

<menu>
   <item title="Home" link="home.php" />
   <item title="Content" link="content.php">
      <item title="Page1" link="page.php?id=1" />
      <item title="Page2" link="page.php?id=2" />
   </item>
</menu>

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.