PHP / XML question

Discussion in 'PHP' started by Teessider_2000, Dec 22, 2008.

  1. #1
    Hi

    With a simple xml file as follows, what would be the best and simplest way in php to edit 2 of the node values?...

    <pages>
    <page id="index">
    <filename>index</filename>
    <pagename>Welcome</pagename>
    <pagearea>home</pagearea>
    <template>homepage</template>
    <keywords />
    <description />
    <order>2</order>
    </page>
    <page id="contact">
    <filename>contact</filename>
    <pagename>Contact</pagename>
    <pagearea>general</pagearea>
    <template>default</template>
    <keywords />
    <description />
    <order>2</order>
    </page>
    </pages>

    Basically this xml stores page information for a simple cms im developing.

    I've been playing around for ages trying to find the best way to update page keywords and description nodes but havent found an easy way to do this. All i want to do is just go to the right page node and edit the keywords and descriptions and then save the xml file. It shouldnt be difficult but its causing me lots of grief.

    Any help would be greatly recieved.

    Cheers, Will
     
    Teessider_2000, Dec 22, 2008 IP
  2. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    look at the dom xml functions on php.net, it can convert xml objects into php arrays, and back into xml again for you.
     
    powerspike, Dec 22, 2008 IP
  3. Teessider_2000

    Teessider_2000 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've had a quick look but it seems an awful lot of code to do something relatively simple. Does anyone know a good example i could use to start myself off?
     
    Teessider_2000, Dec 23, 2008 IP
  4. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    DOM is your best bet. The simplest way to edit any node is to use XPath. Just check my article about .

    When you get node you need with xpath then just change it value (eg $node->nodeValue = "something"; ) All objects are passed by refernece so changes will be made in main document as well. Then just use DomDocument::save() method and that's.

    It's just few lines of code if you know about xpath :)
     
    Greg Carnegie, Dec 23, 2008 IP