简体   繁体   中英

update key value in xml with php

I'm not sure if it's possible or not, but here is what I would like to be able to do.

My xml file structure:

<?xml version="1.0" encoding="utf-8" ?>
<content>
  <option name="0">Yes</option>
  <option name="1">No</option>
  <option name="2">Maybe</option>
</content>

I would like to update only one node at the time, for example: Replace word "Maybe" with something else, where value == 2. So it needs to search for "option name="2" and replace word Maybe with user input.

You can do that with simplexml and xpath :

$xml = simplexml_load_string($x); // assume XML in $x

// get the node
$node = $xml->xpath("/content/option[@name = '2']");

// change it
$node[0][0] = "Hello!";

see it working: https://eval.in/127855

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM