To modify the xml file value, I want to change the value of question to 22222. this value is passed in at the front end, but I don't know how to transfer it. how should I do it? It is best to use the DOMDocument class for operations.
3 admin
111111
A
11
B
22
C
33
D
44
22222
A
22
B
11
C
44
D
33
33333
A
444
B
222
C
333
D
2111
Reply to discussion (solution)
How do you change it?
$ Fn = 'Your xml filename '; $ tag = 'question'; // The name of the node to be modified $ val = '000000'; // The value of the node to be modified, used together with $ tag to locate $ replace = 'XXX'; // The modified value $ xml = simplexml_load_file ($ fn ); $ p = $ xml-> xpath ("// {$ tag }[. = '$ val'] "); $ p [0] [0] = $ replace; $ xml-> asXML ($ fn); // write back
PHP code? 12345678 $ fn = 'Your xml filename '; $ tag = 'question'; // The name of the node to be modified $ val = '000000'; // The value of the node to be modified, used together with $ tag to locate $ replace = 'XXX'; // The modified value $ xml = simplexml_load_file ($ fn ); $ p = $ xml-> xpath ("// {$ tag }[. = ......
$ P [0] [0] what does it mean?
Xpath returns an array of all conforming nodes (a two-dimensional array of objects)
$ P [0] [0] indicates the first matched node
$ P [0] [0] indicates the first element in the node.
Xpath returns an array of all conforming nodes (a two-dimensional array of objects)
$ P [0] [0] indicates the first matched node
$ P [0] [0] indicates the first element in the node.
In this case, the question is 111111, not 222222.
$ Xml-> xpath ("// {$ tag} [. = '$ VAL']");
The actual execution is
$ Xml-> xpath ("// question [. = '000000']");
Therefore, your data will only match 22222