简体   繁体   English

使用JDOM2解析android清单文件

[英]parsing an android manifest file with JDOM2

hope you're fine. 希望你没事。 I have to parse an android manifest file to extract data like 'the minSdkVersion' used, after several seraches I found a code using JDOM. 我必须解析一个android清单文件来提取像'minSdkVersion'这样的数据,经过几次seraches后我发现了一个使用JDOM的代码。 Display data related to "uses-sdk" was expected but When running I got a null object. 显示与“uses-sdk”相关的数据是预期的但是在运行时我得到了一个空对象。 Need Help Thanks 需要帮助谢谢

Manifest Example 清单示例

<?xml version="1.0" encoding="UTF-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.rssfeed"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="19" />

        <uses-permission android:name="android.permission.INTERNET" />

     </manifest>

The code 编码

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;

public class ManifestP2 {

    static Document document;
    static Element root;
    static NeededTag tags;//Data receiver

    public static void main(String[] args){
        SAXBuilder builder = new SAXBuilder();
        try{

            document = builder.build(new File("AndroidManifest.xml"));

        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        root = document.getRootElement();
        tags = new NeededTag();
        findTag();
        System.out.println(tags.usesSdk.toString());
    }

     static void findTag(){
         //get values for "uses-sdk" tag    
        Element sdk = root.getChild("uses-sdk");
        tags.usesSdk.minSdk = sdk.getAttributeValue("android:minSdkVersion");
        tags.usesSdk.targetSdk = sdk.getAttributeValue("android:targetSdkVersion");

    }
}

The attributes in your document are in the android namespace. 文档中的属性位于android命名空间中。 Namespaces in XML add a level of complexity, that's true, but they also ensure that you can have special types of documents, like your manifest, where you can store "meta" types of information inside the same document as regular data, while still guaranteeing that there won't be any name collisions. XML中的命名空间增加了一定程度的复杂性,这是正确的,但它们也确保您可以拥有特殊类型的文档,例如清单,您可以将“元”类型的信息存储在与常规数据相同的文档中,同时仍然保证没有任何名称冲突。

That's a complicated way of saying that where you see android in your XML document, it refers to a namespace, not the attribute name. 这是一种复杂的说法,你可以在XML文档中看到android ,它指的是命名空间,而不是属性名称。

So, for example, where you have: 所以,例如,你有:

  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19" /> 

that really means you have your own element uses-sdk and that element has 2 attributes, one called minSdkVersion , and the other called targetSdkVersion . 这实际上意味着你有自己的元素uses-sdk ,该元素有2个属性,一个叫做minSdkVersion ,另一个叫做targetSdkVersion Those attributes are in the namespace with the URI: http://schemas.android.com/apk/res/android . 这些属性位于带有URI的名称空间中: http://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/android

To get those attributes off the element in JDOM, you have to ask for them by name, and in the right namespace. 要从JDOM中的元素中获取这些属性,您必须按名称和右侧命名空间请求它们。

To do that, you have the code: 要做到这一点,你有代码:

    Namespace ans = Namespace.getNamespace("android", "http://schemas.android.com/apk/res/android");
    String min = sdk.getAttributeValue("minSdkVersion", ans);
    String target = sdk.getAttributeValue("targetSdkVersion", ans);

You declare the namespace to search for (only the URI is important when searching, the android namespace prefix is there for convenience only) 你声明要搜索的命名空间(搜索时只有URI很重要, android命名空间前缀只是为了方便)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM