0

I have a manifest.plist file (come from Apple). This is an XML file. Here's an exemple of the structure :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>id</key>
    <string>3214</string>
    <key>name</key>
    <dict>
      <key>en</key>
      <string>Hello World</string>
      <key>jp</key>
      <string>Hello World JP</string>
    </dict>
    <key>kilometers</key>
    <integer>430</integer>
    <key>cloud</key>
    <true/>
  </dict>
</plist>

I can get this XML as object with simplexml. Now I would like to change some values in my XML (e.g cloud to false of jp string value).

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Pierre
  • 10,593
  • 5
  • 50
  • 80
  • 1
    OK, but what is your question? Is it just how to edit the XML? Then searching `php edit xml` will give you everything you need – Pekka Mar 08 '13 at 10:36
  • Now I tried with DOMElement and Xpath query '/plist/dict/key[4]' to get the cloud key. But How can change its value to false ? – Pierre Mar 08 '13 at 11:02

1 Answers1

1

Now I tried with DOMElement and Xpath query '/plist/dict/key[4]' to get the cloud key. But How can change its value to false ?

The element you talk about is here:

    <key>cloud</key>
    <true/>

And you want to change from <true/> to <false/>. However that is not changing the value but replacing the <true/> element node with a new node, a <false/> element node.

This is not (really) possible with SimpleXML because it can not replace nodes.

With DomDocument you can do it with the DOMNode::replaceChild()Docs function.

An example:

Let's assume you have the variable $key and it is the <key> element you've fetched via xpath.

$true  = $key->nextSibling;
$false = $key->ownerDocument->createElement('false');
$key->parentNode->replaceChild($false, $true);
hakre
  • 193,403
  • 52
  • 435
  • 836