Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java的XML文件深层读取特定元素_Java_Android_Xml_Parsing_Dom - Fatal编程技术网

从java的XML文件深层读取特定元素

从java的XML文件深层读取特定元素,java,android,xml,parsing,dom,Java,Android,Xml,Parsing,Dom,我必须读取一个android清单文件,如下所示 <?xml version="1.0" encoding="utf-8"?> <manifest android:versionCode="92" android:versionName="1.9.7B92" package="com.rechild.advancedtaskkiller" xmlns:android="http://schemas.android.com/apk/res/android"> &l

我必须读取一个android清单文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="92" android:versionName="1.9.7B92" package="com.rechild.advancedtaskkiller"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:label="@string/app_name" android:icon="@drawable/icon2">
        <activity android:label="@string/app_name" android:name=".AdvancedTaskKiller" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.rechild.intent.action.ADVANCED_TASK_KILLER_FREE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".NewSettings" />
        <activity android:name=".IgnoreListActivity">
            <intent-filter>
                <action android:name="com.rechild.advancedtaskkiller.intent.action.IgnoreList" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".HealthActivity" />
        <activity android:name=".LogActivity" />
        <activity android:name=".HelpActivity" />
        <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation" />
        <receiver android:name=".AutoStartReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".BackService" android:exported="true" />
        <receiver android:label="@string/app_name" android:icon="@drawable/icon2" android:name=".OneClickAppWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider" />
        </receiver>
        <receiver android:name=".AlarmReceiver" />
    </application>
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-sdk android:minSdkVersion="3" />
    <supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" />
</manifest>

我的目标是读取所有活动元素中android:name属性的值。在java中如何实现这一点?

我解决了这个问题,这是我的代码

package mytest;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author 
 */
public class ManifestParser {

    public static void Parse(String fileName) {

    try 
    {
        File file = new File(fileName);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        System.out.println("Root element " + doc.getDocumentElement().getNodeName());
        NodeList nodeLst = doc.getElementsByTagName("application");
        System.out.println("Information of all activities");

        for (int s = 0; s < nodeLst.getLength(); s++) {


            Node fstNode = nodeLst.item(s);

          if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

            Element fstElmnt = (Element) fstNode;
            NodeList fstNmElmntLst = fstElmnt.getChildNodes();
            int sz=fstNmElmntLst.getLength();


            for(int nodes=0;nodes<sz;nodes++)
            {
                Node tempNode=fstNmElmntLst.item(nodes);
                if (tempNode.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element tmpElmnt = (Element) tempNode;
                    NamedNodeMap nn=tmpElmnt.getAttributes();

                    if(tmpElmnt.getNodeName().equals("activity"))System.out.println("a: "+tmpElmnt.getAttribute("android:name"));
                }
            }

          }

       }
    } catch (Exception e) {
      e.printStackTrace();
    }
   }



}

使用Xml解析器,如Sax解析器或JAXB