Jump to content

[SOLVED] Editing Particular XML Child Elements without using an array ID.


Recommended Posts

Hi,

 

I only have a basic understanding of Objects, so bare with me if I use the incorrect terminology or don't explain my problem effectively.

 

Objective

To insert HTML data from an textarea into an XML file.

 

Conditions

The data inserted must correspond to particular child elements within the XML based on 2 attributes 'URL' and 'Title'.

 

Example XML

 

<website>
<page url="index.php">
	<subpage title="one">
		<header>Header</header>
		<paragraphone>Paragraph1</paragraphone>
		<paragraphtwo>paragraph2</paragraphtwo>
		<paragraphthree>paragraph3</paragraphthree>
	</subpage>
</page>
</website>

 

Clarification

I've tried using both DOMXPath and SimpleXMLElement to achieve this, but after searching for documentation I am unable to find an example piece of code on how to conditionally edit the text inside <header> (etc.) based on 'URL' and 'Title'.

 

I've read the following guide, which helped my understand the general concept when handling XML data, but unfortunately I've not been able to make practical use of his examples with this conditions.

 

http://www.phpfreaks.com/tutorial/handling-xml-data

 

I've also checked through the PHP Documentation but again, nothing mentioned about editing based on conditions (other than an array ID).

 

I have limited code, basically just opening the XML file and creating the object. I'm also passing in the values URL and Title using AJAX, other than that I am stuck.

 

The Big Q

Does anyone know of an example or guide I can look at, or that they can post for me to see?

 

Thanks in advance,

Solved myself using the following code.

 

$doc = file_get_contents('../cms/page-content.xml');

$xml = new SimpleXMLElement($doc);

$result = $xml->xpath("/website/page[@url='$page']/subpage[@title='$subpage']");

$result[0]->header = "Header";
$result[0]->paragraphone = "Paragraphone";

$xml->asXML('../cms/page-content.xml');

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.