解析AndroidManifest属性

时间:2012-09-17 15:41:09

标签: php android android-manifest

我用PHP制作了一个简单的apk上传器,我想从AndroidManifest.xml解析清单属性,但我得到了以下错误,任何想法!?

Fatal error: Call to a member function xpath() on a non-object in E:\AppServ\www\apkUploader\mainApi.php on line 21 

第21行= $versionName = $xml->xpath('/manifest/@android:versionName');

源代码:

/* For Apk File */

if (($_FILES["mFile"]["type"] == "application/vnd.android.package-archive") || ($_FILES["mFile"]["type"] == "application/octet-stream"))
{
    if ($_FILES["mFile"]["error"] > 0)
      {
          echo "Return Code: " . $_FILES["mFile"]["error"] . "<br />";
      }
    else
      {
          echo "Upload: " . $_FILES["mFile"]["name"] . "<br />";
          echo "Type: " . $_FILES["mFile"]["type"] . "<br />";
          echo "Size: " . ($_FILES["mFile"]["size"] / 1024) . " Kb<br />";
          echo "Temp file: " . $_FILES["mFile"]["tmp_name"] . "<br />";
          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/>";
          if (file_exists("upload/" . $_FILES["mFile"]["name"]))
            {
              echo $_FILES["mFile"]["name"] . " already exists. ";
            }
          else
            {
              move_uploaded_file($_FILES["mFile"]["tmp_name"],
              "upload/" . $_FILES["mFile"]["name"]);
              echo "Stored in: " . "upload/" . $_FILES["mFile"]["name"];
            }
      }
  }
else
  {
      echo "Invalid file";
  }

1 个答案:

答案 0 :(得分:1)

看起来$xml = simplexml_import_dom($dom);没有返回有效对象。这通常意味着无法加载或解析文件。

不要忘记检查是否有错误:

$xml = simplexml_import_dom($dom);
if (!$xml)
{
    // An error occurred
}