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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.