问题
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).
回答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);
来源:https://stackoverflow.com/questions/15291969/change-manifest-plist-values-in-php