我想从AndroidManifest.xml中获取版本名称、版本代码和包名称。到目前为止,我拥有的是:
// Load AndroidManifest.xml
$xml = simplexml_load_string($manifest);
//print_r($xml);
list(,$versionCode) = each($xml->xpath('/manifest/@android:versionCode'));
list(,$versionName) = each($xml->xpath('/manifest/@android:versionName'));
list(,$package) = each($xml->xpath('(/manifest/@package)'));
echo "versionCode: ".$versionCode.", versionName: ".$versionName.", package:".$package."'n";
我想知道是否有更好的方法来获得这些元素的值?
示例AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1" android:versionName="1.0" package="com.example.bluetoothchat"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".BluetoothChat" android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/select_device" android:name=".DeviceListActivity" android:configChanges="keyboardHidden|orientation" />
</application>
</manifest>
您可能需要使用PHP XQuery扩展来完成这项工作:
declare namespace android = "http://schemas.android.com/apk/res/android";
let $manifest := (: your XML :)
return
<ul>
<li>{$manifest/@android:versionCode/string()}</li>
<li>{$manifest/@android:versionName/string()}</li>
<li>{$manifest/@package/string()}</li>
</ul>
你可以在http://www.zorba-xquery.com/html/demo#fQYhs9ga8WtwEr3ybERv9hftRnw=有关如何使用PHP安装XQuery扩展的说明,请访问http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery
检查这是预期结果
<?php
error_reporting(E_ERROR | E_PARSE);
$dom = new DOMDocument();
$dom->load('AndroidManifest.xml');
$xml = simplexml_import_dom($dom);
$versionName = $xml->xpath('/manifest/@android:versionName');
$versionCode =$xml->xpath('/manifest/@android:versionCode');
$package = $xml->xpath('/manifest/@package');
echo "VERSION NAME :".$versionName[0]->versionName."<br/>";
echo "VERSION CODE :".$versionCode[0]->versionCode."<br/>";
echo "VERSION PAC :".$package[0]->package."<br/>";
?>