user3176519
user3176519

Reputation: 403

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.

Upvotes: 0

Views: 93

Answers (1)

michi
michi

Reputation: 6625

You can do that with simplexmland 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

Upvotes: 2

Related Questions