Jump to content

Append XML data with PHP with form


boozker

Recommended Posts

I'm writing a super lightweight CMS tool that does not need a database because it uses XML (cms.xml) for the web page content. I got it to parse the XML and make it all nice valid XHTML, but now I need to write to that exact same XML file with a form. Such as the one I have here

 

<form method="post" action="newpage_submit.php">
	<input type="text" value="Title" name="title"/><br />
	<input type="text" value="Sub Header" name="subheader" /><br />
	<input type="text" value="Content Here" name="content"/><br />
	<input type="submit" name="save" />
</form>

 

This will allow the users to create a new page. The form gets passed to newpage_submit.php where I grab the $_POSTs and make them into variables to be used in the XMLWriter function to put inside the XML tags I have specified in the XML sheet for the website.

 

I need it to parse and append the cms.xml XML sheet. Here is what the xml sheet looks like barebones:

 

<website>
<webpage>
	<title></title>
	<subhead></subhead>
	<content>
		<p></p>
	</content>
</webpage>
</website>	

 

The website tag is the root and then I have the webpage tag which is, well, a webpage and all the information for that page. I have it loop through the p tags and then the page is done.

 

The $_POST data will be put into the XMLWriter and then sent to that XML sheet and it sending and saving (appending) the XML sheet is where I am stuck. If you want more of my code let me know. I have no idea how to get it to post the xml data. I am still very new to PHP and have only worked with it for about a month.

Link to comment
Share on other sites

I am using simplexml to parse it. Here is some more of my code

 

//This will get the xml ready. Always include!
$xmlFileData = file_get_contents('cms.xml');
//XML Parser
$xmlData = new SimpleXMLElement($xmlFileData);
//Find out what page we are on
$webpage = $xmlData->webpage[$page_id];

 

the page id is on every new page and the number is incremented up with each new page (or it will I have yet to write this until I can create a new page)

 

Also, I do not want to use XSLT. This is going to be a CMS tool that you will be able to install and just add a page_content(); function I made and that will go through all the loops and other functions and parse the XML out for you. All you would have to do is edit in the CMS tool. Plus it would it take longer for me to rewrite everything since it already parses the data. I do not need it to parse it I need the data to amend TO a XML sheet.

Link to comment
Share on other sites

It might be easier to just rebuild the XML file every single time.  I have an application I wrote that has a simple little xml file that stores file paths for users.  Everytime the user adds, edits, or deletes the XML file I just completely rebuild the XML file.

 

So I guess maybe creating arrays of the data that will go into the xml, unsetting the piece of information that has been removed, and then rebuilding the XML file could work.  The same would be for saving content.  The difficult portion is what if they add an element in the middle of your XML file.  That gets trickier, you basically have to take your base array, build a new array and stick that piece of information in it, and then loop through writing to the XML.

 

Or perhaps I am just misinterpreting what you are looking for.

Link to comment
Share on other sites

The problem is, is that I don't know how to add content to the top below the root or at the bottom right before the end root tag. Even if I brought that entire XML sheet over, how would I add to it? I can open it and read it by reading the XML file, but I can't ADD content between the root tags.

 

To make it easier to understand, how would I do this? This file, for example, myfile.xml has the Before code in it now, how to I put the after code inside with PHP5.

 

Before

 

<root>
    <node atr="1">Node 1</node>
    <nodeTwo></nodeTwo>
</root>

 

After

 

<root>
    <node atr="1">Node 1</node>
    <nodeTwo>
        <subNode>SubNode</subNode>
    </nodeTwo>
    <nodeThree>Node 3</nodeThree>
</root>

Link to comment
Share on other sites

  • 2 weeks later...

Really?! No one knows how to append xml with php5 at phpfreaks?  :o

 

Well, I waited about 2 weeks and no response, so I'm bumping it because I know others have this question as well since I have been researching it. I'm going to go post on other forums as also, but I never thought that this was going to be that hard.

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.