0

i want to make php find a node in xml by the value, and change it...

my xml is the following :

<?xml version="1.0" encoding="ISO-8859-1"?>
<pages>
<link>
<title>App1</title>
<id>c4ca4238a0b923820dcc509a6f75849b</id>
<downloadsnumber>0</downloadsnumber>
</link>
<link>
<title>App2</title>
<id>c81e728d9d4c2f636f067f89cc14862c</id>
<downloadsnumber>0</downloadsnumber>
</link>
<link>
<title>App3</title>
<id>eccbc87e4b5ce2fe28308fd9f2a7baf3</id>
<downloadsnumber>0</downloadsnumber>
</link>
<link>
<title>App4</title>
<id>a87ff679a2f3e71d9181a67b7542122c</id>
<downloadsnumber>0</downloadsnumber>
</link>
</pages>

My php code has to search for an ID (c81e728d9d4c2f636f067f89cc14862c for example, which is App2), and then it has to change the value of <downloadsnumber> within that same <link> node to 1.

How can i do this with php ?

Thanks

Evert

Evert De Spiegeleer
  • 177
  • 1
  • 3
  • 10
  • 1
    This question is already asked here http://stackoverflow.com/questions/2956601/change-xml-node-element-value-in-php-and-save-file?rq=1 – Thijmen Venus Jul 05 '13 at 10:05
  • @ThijmenVenus Yeah.. But i don't want to put the xml in a php file... It has to stay in a xml file... – Evert De Spiegeleer Jul 05 '13 at 10:24
  • @EvertDeSpiegeleer: If that is your problem then you need to ask that specifically (and it likely will get closed because that's just the same method only with a filename parameter). – hakre Jul 05 '13 at 13:46

2 Answers2

0

I would hav done it like this:
1. Use simpleXml to convert your xml to a php object.
2. Change the line as in a regular object.
3. Save the complete XML.

http://www.php.net/manual/en/simplexml.examples-basic.php

  • Thanks.. I know how to do that, but the problem is to edit THE RIGHT value.. the one under the selected ID... – Evert De Spiegeleer Jul 05 '13 at 10:26
  • @EvertDeSpiegeleer: The linked duplicate shows that based on an attribute value (of which an element is the parent), you might need to adopt that to something called a *sibling* in XML: [DOMXPath get sibling depending on previous sibling value](http://stackoverflow.com/q/7139335/367456) – hakre Jul 05 '13 at 13:58
0

The link i posted is just to show you how you can change certain node you dont have to save it anywhere