在PHP中更改manifest.plist值 [英] Change manifest.plist values in PHP
问题描述
我有一个manifest.plist文件(来自Apple).这是一个XML文件. 这是结构的一个例子:
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>
我可以使用simplexml将此XML作为对象.现在,我想更改XML中的某些值(例如,将cloud更改为jp字符串值的false).
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).
推荐答案
现在,我尝试使用DOMElement和Xpath查询'/plist/dict/key [4]'获取云密钥.但是如何将其值更改为false?
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 ?
您谈论的元素在这里:
<key>cloud</key>
<true/>
您想从<true/>
更改为<false/>
.但是,这不是 更改值,而是将<true/>
元素节点替换为新节点,即<false/>
元素节点.
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.
SimpleXML
不可能(真的)可行,因为它不能替换节点.
This is not (really) possible with SimpleXML
because it can not replace nodes.
使用DomDocument
,您可以使用 DOMNode::replaceChild()
Docs 函数.
With DomDocument
you can do it with the DOMNode::replaceChild()
Docs function.
一个例子:
假设您具有变量$key
,它是您通过xpath获取的<key>
元素.
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);
这篇关于在PHP中更改manifest.plist值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!