php从androidmanifest.xml获取manifest属性

时间:2012-02-24 17:53:03

标签: php android xml

我想从AndroidManifest.xml获取versionName,versionCode和package名称。 这是我到目前为止所做的:

// 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>

2 个答案:

答案 0 :(得分:0)

检查这是预期的结果

<?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/>";
?>

答案 1 :(得分:0)

您可能希望使用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