fatboymuzzy Posted September 8, 2009 Share Posted September 8, 2009 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, Quote Link to comment https://forums.phpfreaks.com/topic/173520-solved-editing-particular-xml-child-elements-without-using-an-array-id/ Share on other sites More sharing options...
fatboymuzzy Posted September 8, 2009 Author Share Posted September 8, 2009 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'); Quote Link to comment https://forums.phpfreaks.com/topic/173520-solved-editing-particular-xml-child-elements-without-using-an-array-id/#findComment-914779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.