-1

Possible Duplicate:
A simple program to CRUD node and node values of xml file

I am having trouble updating nodes in an xml file. My xml files is like this:

    <container id="header"> 
        <widgetname="header" cache='true' cachetime='7200' ><parameter name="image" value="gnome"></parameter></widget>
    </container>

    <container id="nav_wrapper">    
        <container id="main_menu_container">
            <widget name="menu">
                <parameter name="menuid" value="9"></parameter>
            </widget>
        </container>
        <container id="search_wrapper">
            <widget name="search" cache='true' cachetime='7200' ></widget>
        </container>
    </container>

    <container class="subpage_wrapper">
        <container id="leftcol">
            <container class="subnav">
                <widget name="menu">
                    <parameter name="menuid" value="5"></parameter>
                </widget>
            </container>
        </container>
        <container id="rightcol">
            <page name="articlelist"></widget>
        </container>
    </container>
</container>
</layout>

The layout for the whole page will be rendered according to this xml, which is working properly. The trouble starts when the admin have to update the parameters to the widgets. For that I have provided a preview layout and the admin can select the parameters to the widgets.

For e.g. When the admin click config for the container>topbar>top_nav>menu widget he will be provided with all the available options. But I haven't found any solution for saving the newly selected value to that node.
Does anyone have any idea how to do that?

[Edit] The main issue is that the child element can be infinite. Also there may be more than one child for the same path container>container>widget will repeat may times in the xml

Community
  • 1
  • 1
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47

1 Answers1

1

There are many options for editing XML in PHP.

Take a look at PHP DOM or SimpleXML.

The SimpleXML documentation has a lot of examples. One of the examples is writing values to the XML document.

jimp
  • 16,999
  • 3
  • 27
  • 36
  • I have checked both the links. Neither of it shows how to get to the specified node, especially in an xml structure like one above. Can you provide some code snippet or some links that explains it? – Nandakumar V Oct 02 '12 at 05:36
  • Did you see my 3rd link? It has a lot of code examples. I thought the one about writing a value would help. http://www.php.net/manual/en/simplexml.examples-basic.php#example-5150 – jimp Oct 02 '12 at 06:04
  • Thank you @jimp. That examples helped me to update the node attributes in xml. But i am still having an issue in finding the correct node to be updated. In all those examples the xml structure is static. But in my case the levels are dynamic ie there may be infinite 'container' nodes. so the specifying the node path `$movies->movie->characters->character` is not possible. – Nandakumar V Oct 03 '12 at 05:54
  • Also @gordon, thanks for specifying the post [Possible Duplicate](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file). But primary issue here was not "how to update", but "to finding the correct node to update". Apologize if the title made the confusion. And i still haven`t got the solution. – Nandakumar V Oct 03 '12 at 06:03
  • @NandakumarV Check into the [SimpleXML XPath example](http://www.php.net/manual/en/simplexml.examples-basic.php#example-5156). You might still need to use some programming logic to handle your infinite container nodes, but that will help you find the node to update. – jimp Oct 03 '12 at 15:03
  • thanks jimp, but i used the eval() function to update the node? It is not a best practice though, but surely did the trick. – Nandakumar V Oct 11 '12 at 05:25
  • I'm not sure how you did that with PHP, but I'm glad you found a solution that worked for you. – jimp Oct 11 '12 at 15:37