-1

我正在开发一个 android 项目,但我无法解析本地文件 (raw/file.xml)。我尝试了一些我在互联网上找到的建议,但徒劳无功。当我尝试在网络中解析文件(使用 URL 类...)时它可以工作,但是当我尝试使用本地文件程序时它不起作用!!!请我需要帮助。(我使用 Sax 作为 XML 解析器)

我将行 URL 替换sourceUrl = new URL ("example.com/w...";);InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

xr.parse(new InputSource(sourceUrl.openStream()));和线xr.parse(new InputSource(is.getByteStream()));

4

1 回答 1

0
 You can put the xml file path here and keep parsing the inner nodes usiln self running loop. Save the values in arrays accordingly for each node..

    NodeList eNodeList = null;
eNodeList = Utils.getXMLFromFilePath(your.xml", "firstNOdeNAme", eNodeList);
getElementsByNodelist(eNodeList)


    private static getElementsByNodelist(Nodelist nodelist)
{
    if(nodelist!=null)
    {
        for(int i = 0;i<nodelist.getLength();i++)
        {
            Element element = (Element) nodelist.item(i);
            String id=getTextValue(element, "id");
            //For attribute value
            element.getAttribute("attributename");
            // if u want to parse tjis element again 
            NodeList l=element.getElementsByTagName("innerTag");
            // self loop
            getElementsByNodelist(l);
        }
    }

}

public static String getTextValue(Element ele, String tagName) {
    String textVal = "";
    NodeList nl = ele.getElementsByTagName(tagName);
    if(nl != null && nl.getLength() > 0) {
        Element el = (Element)nl.item(0);
        if(el.getFirstChild()!=null)    
        {
            textVal = el.getFirstChild().getNodeValue();
        }
    }
    return textVal;
}
于 2012-07-10T14:32:10.133 回答