Jump to content

using simpleXML to edit a node attribute


bdee1

Recommended Posts

i am using simpleXML to parse and update the contents of an xml file based on user input.

 

my xml structure looks liek this:

<linkCollection>
	<linkGroup id="1" title="Photoshop">
		<linkItem id="2">
			<title></title>
			<description></description>
			<url></url>
		</linkItem>
		<linkItem id="4">
			<title></title>
			<description></description>
			<url></url>
		</linkItem>
	</linkGroup>
</linkCollection>

 

i want the user to be able to edit the title attribute of the linkGroup node.  i know that i could drop the node and then rebuild it with the updated values but since the linkGroup node could have many children, it woudl be nice if i coudl just update the title attribute and then save the file.

 

how could i go about doing that?

 

nevermind - it was way simpler than i thought.

 

#CREATING THE HANDLER
$xml = simplexml_load_file(SITEROOT.'/data/file.xml');

#loop through the LinkGroups to find the one we're looking for
foreach($xml->linkCollection->linkGroup as $linkGroup)
{
	if ($linkGroup['id'] == $passed_groupID)
	{
		$linkGroup['title'] = $passed_title;
		break;
	}
}

#WRITE THE RESULTS BACK TO THE XML FILE
file_put_contents(SITEROOT.'/data/file.xml', $xml->asXml());

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.