Android 不能';t打开资产/sample.xml

Android 不能';t打开资产/sample.xml,android,Android,我收到错误“无法打开资产/sample.xml” 这是我的密码: try { // creates and returns new instance of SAX-implementation: SAXParserFactory factory = SAXParserFactory.newInstance(); // create SAX-parser... SAXParser parser = f

我收到错误“无法打开资产/sample.xml”

这是我的密码:

try {
            // creates and returns new instance of SAX-implementation:
            SAXParserFactory factory = SAXParserFactory.newInstance();

            // create SAX-parser...
            SAXParser parser = factory.newSAXParser();
            // .. define our handler:
            SaxHandler handler = new SaxHandler();

            // and parse:
            parser.parse("assets/sample.xml", handler);

        } 

资产不是文件。您需要调用
Context.getAssets().open(“sample.xml”)
并将结果输入流传递到
parse()
尝试调用parser.parse(“file:///android_asset/sample.xml“,处理程序)

如果要使用SAX解析器,这是正确的答案。尽管从实际的上下文实例调用getAssets(),但您应该按照@Michael所说的方式来做,但我建议您使用简单的XML。这意味着您甚至不必触摸SAX解析器就可以将XML导入文档。请看我的博客文章,它让您知道如何在android中获得它: