0

So I have a web page that displays the data of an XML file. However I have a form as well that should ideally be able to change the data. I figured the best way to accomplish this would be to use the Fopen function in PHP and edit the specific line of Text. The string won't be long, 4 words max. How would I use the fopen and the series of functions that open, read and write files to find a line of code and replace it?

  • I don't see an easy way to achieve what you are trying to do but, Is the XML file a very big file? Is it possible to POST the entire XML data back to the server and update only the XML nodes that have changes? – juanreyesv May 13 '13 at 00:01
  • To handle the XML you can use the SimpleXML library. Here is a post about how to use it: http://stackoverflow.com/questions/2092172/edit-xml-with-simplexml – juanreyesv May 13 '13 at 00:04
  • @juanreyesv the XML file is small only 27 lines long itself but the actual data is only 12 phrases. It's a really short XML file. – user2235005 May 13 '13 at 00:22
  • I would also like to make note that I am using Simple XML to define my function. – user2235005 May 13 '13 at 00:22
  • Ok, you want to change the values that are inside the XML tags, right? – juanreyesv May 13 '13 at 00:28
  • That's exactly what I wish to do – user2235005 May 13 '13 at 00:35

3 Answers3

1

I think you should do something like the following:

  1. Match the name of your input tags in your HTML to the nodes in your XML (this is only a suggestion to have your code more organized)

  2. When you post the data back to your server, in your Php code, using SimpleXML you can do as following

Giving the following XML file

<root>
    <config id="1">
        <name>old name</name>
        <category>old category</category>
    </config>
    <config id="2">
        <name>old name</name>
        <category>old category</category>
    </config>
</root>

//Load the XML file
$xml = simplexml_load_file('PATH TO YOUR XML FILE');
//Update the values that you want to update
foreach($xml->root->config as $configGroup)
{
    //Set the $IdToUpdate variable with the Id of the group that you want to update.
    if ($configGroup['id'] == $IdToUpdate)
    {
        $configGroup->name = $_POST['name' . $IdToUpdate];//Your html must have the proper input names.
        break;
    }
}

//Save the changes
$xml->asXml('PATH TO YOUR XML FILE');

Hope this helps

juanreyesv
  • 853
  • 9
  • 22
  • Sure does help a lot, though I'm wondering, what if I have multiple tags for other like instead of just having one , I have 3, each with a defined ID. – user2235005 May 13 '13 at 01:04
  • Updated my answer with the example you asked for. Hope this helps – juanreyesv May 13 '13 at 01:29
  • Thanks. However I found an easier and shorter way to deal with my issue. Thanks for your time, I used your previous example for the code and found a shortcut to it. – user2235005 May 13 '13 at 01:29
0

You probably want to start here: http://www.php.net/manual/en/refs.xml.php

Sounds like you need to do a couple things.

i) Parse the xml into an object or array.

ii) Find the element you're wanting to update.

iii) After updating said element, write the xml back to the file.

Nick
  • 390
  • 2
  • 12
  • Hey Nick thanks for your response. I would just like to state that I have a file which lists all creates the objects. Then on my main file I have a code which extracts the data from the XML file and sorts it nicely displays it. – user2235005 May 13 '13 at 00:20
0

Okay the answer to my problem is very easy. If you're like me, basically a newbie to PHP programming then functions and such can get very tricky, or at least to me. When you are using data from an XML, every tag essentially becomes an array.

<configure id="firstblock">
    <name>Name</name>
    <category>Old</category>
</configure>
<configure id="second block">
     <name>Name2</name>
     <category>New</category>
</configure>

Take note how configure acts like an array.

$xml = SimpleXML_load_file('location of xml file here');
$xml->configure[0]->color = $_POST['name1'];
$xml->configure[0]->category = $_POST['category'];
$xml->configure[1]->name =$_POST['name2'];
$xml->configure[1]->category =$_POST['category2'];
$xml->asXml('location of xml file here');