如何从项目资源android解析xml

时间:2022-03-21 17:16:57

I have a XML file (config.xml) in res/xml folder and I need to parse this XML. To do this I use SAXParser. I am trying this way:

我在res / xml文件夹中有一个XML文件(config.xml),我需要解析这个XML。为此,我使用SAXParser。我这样想:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
          //handler definition...
};
Uri configUri = Uri.parse("android.resource://myPackageName/" + R.xml.config);
saxParser.parse(configXML.toString(), handler);

But this doesn't works...

但这不起作用......

The parse method is have the following parameters:

解析方法有以下参数:

uri The location of the content to be parsed.

uri要解析的内容的位置。

dh The SAXDefaultHandler to use.

dh要使用的SAXDefaultHandler。

Should I use an URI or an Uri? Whats is the difference?

我应该使用URI还是Uri?什么是差异?

1 个解决方案

#1


5  

I have a XML file (config.xml) in res/xml folder and I need to parse this XML

我在res / xml文件夹中有一个XML文件(config.xml),我需要解析这个XML

Call getResources().getXml(R.xml.config). You will be handed an XmlResourceParser, which follow the XmlPullParser API. As shown in the XmlPullParser JavaDocs, you wind up with code like this:

调用getResources()。getXml(R.xml.config)。您将获得一个XmlResourceParser,它遵循XmlPullParser API。如XmlPullParser JavaDocs所示,最终得到如下代码:

     XmlResourceParser xpp=getResources().getXml(R.xml.config);
     int eventType = xpp.getEventType();

     while (eventType != XmlPullParser.END_DOCUMENT) {
       if (eventType == XmlPullParser.START_DOCUMENT) {
           // do something
       } else if (eventType == XmlPullParser.START_TAG) {
           // do something
       } else if (eventType == XmlPullParser.END_TAG) {
           // do something
       } else if (eventType == XmlPullParser.TEXT) {
           // do something
       }
      eventType = xpp.next();
     }

 

To do this I use SAXParser

为此,我使用SAXParser

Even if you can get that to work, it will be ~10 times slower than using the XmlResourceParser.

即使你可以使用它,它也会比使用XmlResourceParser慢10倍。

#1


5  

I have a XML file (config.xml) in res/xml folder and I need to parse this XML

我在res / xml文件夹中有一个XML文件(config.xml),我需要解析这个XML

Call getResources().getXml(R.xml.config). You will be handed an XmlResourceParser, which follow the XmlPullParser API. As shown in the XmlPullParser JavaDocs, you wind up with code like this:

调用getResources()。getXml(R.xml.config)。您将获得一个XmlResourceParser,它遵循XmlPullParser API。如XmlPullParser JavaDocs所示,最终得到如下代码:

     XmlResourceParser xpp=getResources().getXml(R.xml.config);
     int eventType = xpp.getEventType();

     while (eventType != XmlPullParser.END_DOCUMENT) {
       if (eventType == XmlPullParser.START_DOCUMENT) {
           // do something
       } else if (eventType == XmlPullParser.START_TAG) {
           // do something
       } else if (eventType == XmlPullParser.END_TAG) {
           // do something
       } else if (eventType == XmlPullParser.TEXT) {
           // do something
       }
      eventType = xpp.next();
     }

 

To do this I use SAXParser

为此,我使用SAXParser

Even if you can get that to work, it will be ~10 times slower than using the XmlResourceParser.

即使你可以使用它,它也会比使用XmlResourceParser慢10倍。