Forum Moderators: coopster

Message Too Old, No Replies

Editing XML with php

Edit XML with php

         

MrGecko

6:39 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



Hello I'm trying to edit xml with php and I'm getting no where searching so maybe you guys can help.
this is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<stuff>
<head>The head</head>
<body>The body</body>
</stuff>

and i'll be changing head to header or what ever this is just an example.
can I do this and how can I

eelixduppy

7:24 pm on Feb 6, 2008 (gmt 0)



If you have PHP5 at your disposal you should really look up and use SimpleXML [php.net] as it is very easy to use and also very powerful when manipulating XML files.

MrGecko

7:37 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



I was looking at SimpleXML and could not find a way to do it can you give me an example?

eelixduppy

7:55 pm on Feb 6, 2008 (gmt 0)



>> changing head to header

Not sure if that can be done with SimpleXML alone, at least. You are going to have to do a simple string replace for something like that. For instance:


[url=http://www.php.net/str-replace]str_replace[/url]("<head>","<header>",$xml_string);

You'd of course have to write this to the XML file after the replacement.

However, if you wanted to add a child, for instance, you could use addChild [us3.php.net]() and it is very effective.

MrGecko

8:03 pm on Feb 6, 2008 (gmt 0)

10+ Year Member



I guess I can do that