【已解决】Android中解析/res/raw文件夹下面的xml文件

【背景】

一个android项目,在项目的/res/raw文件夹下面存在一个xml文件:groupmenu.xml

现在需要加载并解析出来。

【折腾过程】

1.参考:

Providing Resources | Android Developers

找到解释:

 raw/

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

2.然后去看看:

Resources | Android Developers

3.再去参考:

Complete XML reading/parsing on Android Tutorial | ITek Blog

结果出错:

【已解决】Android中使用getResources去尝试解析xml时出错:The method getResources() is undefined for the type

然后就可以通过代码:

InputStream is = AppContext.getInstance().getResources().openRawResource(R.raw.groupmenu);

去获得对应的InputStream了。

4.接下来,就是学习:

Complete XML reading/parsing on Android Tutorial | ITek Blog

得知:

对于得到了xml文件的InputStream后,有三种处理办法:

  • Pull parser
    • XmlResourceParser就是此变体
  • Java’s Simple API for XML (SAX) parser
    • 最常用的就是SAX
  • Document Object Model (DOM) parser

然后打算用SAX去处理xml。搜:

android sax xml

找到:

Android——使用SAX解析XML – guanMac – eoe移动开发者社区

看了后,发现SAX使用上的确很繁琐,所以放弃用SAX了。

5.打算去试试:

Providing Resources | Android Developers

中说的那个:

Resources.openRawResource()

不过暂时不去试了。

6.想要去用XmlPullParser去解析xml,参考了一堆帖子:

andoird — XmlPullParser 的使用 – andyhuabing的专栏 – 博客频道 – CSDN.NET

XML解析——XmlPullParser – 阿弥陀佛 – ITeye技术网站

发现也还是很麻烦,所以也是放弃。

7.想起了:

Complete XML reading/parsing on Android Tutorial | ITek Blog

提到DOM可以用xpath,所以感觉应该会好用些。

所以再去看看DOM方式解析xml。

期间遇到一个bug:

【已解决】Android中用DOM方式解析xml时使用getElementsByTagName去获得节点结果为空

但是暂时规避掉了。。。

8.剩下的,就是可以正常去解析xml了。

给出相关的代码:

//public String getUniversalDataGroup() {
public UIDataGroup getUniversalDataGroup() {
    UIDataGroup uiDataGroup = new UIDataGroup();
    
    //parse xml file to UIDataGroup

    String menuTag = "Menu";
    //String tabMenuTag = "TabMenu";
    //ArrayList<XmlItem> rootMenuItemList= XmlHelper.getXmlItems(groupMenuFilename, menuTag,null);
    //XmlItem rootMenuItem = 

    InputStream inputStream = AppContext.getInstance().getResources().openRawResource(R.raw.groupmenu);
    //ArrayList<XmlItem> rootMenuItemList= XmlHelper.getXmlItems(inputStream, menuTag,null);
    ArrayList<XmlItem> rootMenuItemList= XmlHelper.getXmlItems(inputStream, menuTag);

    return uiDataGroup;
}


public static ArrayList<XmlItem> getXmlItems(InputStream inputStream, String tag){
    ArrayList<XmlItem> xmlItemList = new ArrayList<XmlItem>();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
        Document document = builder.parse(inputStream);
        Element root = document.getDocumentElement();
        
        //Element menuElement = document.getElementById("Menu");
        //NodeList tabNodeList = menuElement.getElementsByTagName("TabMenu");
        
        //NodeList nodeList = root.getElementsByTagName(tag);
        NodeList nodeList = root.getElementsByTagName("TabMenu");
        
        //NodeList nodeList = root.getChildNodes();

        //String namespaceURI = root.getNamespaceURI(); //null
        //NodeList nodeList = root.getElementsByTagNameNS(namespaceURI, tag);
        
        for(int idx = 0; idx < nodeList.getLength(); idx++){
            Node curNode = nodeList.item(idx);
            String nodeName = curNode.getNodeName();
            String nodeValue = curNode.getNodeValue();
            
            //.......
        }
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    return xmlItemList;
}

 

【总结】

Android中解析/res/raw下面的xml文件的相关主题代码如下:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;
import android.util.Xml;

import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

InputStream is = AppContext.getInstance().getResources().openRawResource(R.raw.groupmenu);

ArrayList<XmlItem> xmlItemList = new ArrayList<XmlItem>();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Element root = document.getDocumentElement();

NodeList nodeList = root.getChildNodes();
//NodeList nodeList = root.getElementsByTagName("TabMenu");

for(int idx = 0; idx < nodeList.getLength(); idx++){
    Node curNode = nodeList.item(idx);
    String nodeName = curNode.getNodeName();
    String nodeValue = curNode.getNodeValue();
    
    //get value, add to your var list
}

供参考。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量