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?

 

Link to comment
Share on other sites

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

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.