从 PHP 中的 .plist ios 抓取数据
grab data from .plist ios in PHP
这就是 plist
<?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>New item</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>http://down.weapp.com/apps/zb/002/43/12/21/58cae8840ba0628e304b1118.ipa</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/57x57bb.png</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/512x512bb.png</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.xiaoji.gamecenter</string>
<key>bundle-version</key>
<string>1.5.2</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>小鸡模拟器</string>
</dict>
</dict>
</array>
</dict>
</plist>
我想获取 .ipa url,为此我正在使用 simplexml
$xml=simplexml_load_string($plistString)
现在我会访问它
$xmlPlist->dict->array[0]->string,
但是那里有 2 个字符串,我怎样才能访问 url 一个?
现在这让我感到困惑,我什至以正确的方式访问 plis 吗?我应该提到每个 "dict" 吗?
试试这个:
echo $xml->dict->array->dict->array->dict->string[1];
要搜索 XML,您应该使用 XPath。这段代码应该可以解决问题:
<?php
$xml = new SimpleXMLElement($plistString);
$result = $xml->xpath('//dict[./key/text()="kind" and ./string/text()="software-package"]/key[text()="url"]/following-sibling::string');
$url = (string)$result[0];
那是什么意思?第一:
dict[./key/text()="kind" and ./string/text()="software-package"]
我们比较child elements and their text content;我们正在寻找包含 <key>kind</key>
和 <string>software-package</string>
.
的 <dict>
然后:
/key[text()="url"]/following-sibling::string
我们获取那个 <dict>
元素,并寻找一个子 <key>
元素,其文本内容为 "url",并得到 <string>
元素 following it.
此时我们还有一个 XML 元素,因此我们使用 typecasting 将其转换为字符串。
这就是 plist
<?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>New item</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>http://down.weapp.com/apps/zb/002/43/12/21/58cae8840ba0628e304b1118.ipa</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/57x57bb.png</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/512x512bb.png</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.xiaoji.gamecenter</string>
<key>bundle-version</key>
<string>1.5.2</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>小鸡模拟器</string>
</dict>
</dict>
</array>
</dict>
</plist>
我想获取 .ipa url,为此我正在使用 simplexml
$xml=simplexml_load_string($plistString)
现在我会访问它
$xmlPlist->dict->array[0]->string,
但是那里有 2 个字符串,我怎样才能访问 url 一个? 现在这让我感到困惑,我什至以正确的方式访问 plis 吗?我应该提到每个 "dict" 吗?
试试这个:
echo $xml->dict->array->dict->array->dict->string[1];
要搜索 XML,您应该使用 XPath。这段代码应该可以解决问题:
<?php
$xml = new SimpleXMLElement($plistString);
$result = $xml->xpath('//dict[./key/text()="kind" and ./string/text()="software-package"]/key[text()="url"]/following-sibling::string');
$url = (string)$result[0];
那是什么意思?第一:
dict[./key/text()="kind" and ./string/text()="software-package"]
我们比较child elements and their text content;我们正在寻找包含 <key>kind</key>
和 <string>software-package</string>
.
<dict>
然后:
/key[text()="url"]/following-sibling::string
我们获取那个 <dict>
元素,并寻找一个子 <key>
元素,其文本内容为 "url",并得到 <string>
元素 following it.
此时我们还有一个 XML 元素,因此我们使用 typecasting 将其转换为字符串。