-2

How can I change the content some nodes in an XML file programmatically using PHP?

The important point is, that the file contains comments and white-spaces, which should be preserved.

Example of the file:

<!-- my config -->
<config>
    <global>
    <install>
        <date/>
    </install>
    <resources>
        <default_setup>
            <connection>
                <host>localhost</host>
                <username/>
                <password/>
                <dbname>foo</dbname>
                <!-- <dbname>bar</dbname> -->
                <model>mysql4</model>

...
Alex
  • 32,506
  • 16
  • 106
  • 171
  • You didn't really ask a question, but you can both use the `DOM` or `SimpleXML` to do this. – Evert Jun 18 '13 at 13:09
  • 5
    5k reputation, you should know SO rules better... your question is too vague and doesn't show any effort. What have you done so far? – Rolando Isidoro Jun 18 '13 at 13:09
  • 2
    I would use a text editor. Or do you need to do this programatically? – LarsH Jun 18 '13 at 13:30
  • [Parsing and processing HTML/XML?](http://stackoverflow.com/questions/3577641/parsing-and-processing-html-xml) and [Basic SimpleXML usage](http://php.net/simplexml.examples-basic) – hakre Jun 18 '13 at 14:09
  • @LarsH: programatically of course :-) *edit* – Alex Jun 19 '13 at 09:23
  • @RolandoIsidoro: It was too hot yesterday... edited the question ... – Alex Jun 19 '13 at 09:26

1 Answers1

3

The easiest way is using SimpleXmlElement. You can load your xml, modify tags and save it as a new xml again.

Here is some good examples about it.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Hossein Shahdoost
  • 1,692
  • 18
  • 32
  • Okay I totally did not consider the option SimpleXml loading and exporting it via `asXML()` does preserve comments. That totally works. – Alex Jun 19 '13 at 09:28