【问题标题】:Android XML parsing, Documentbuilder throws exceptionAndroid XML解析,Documentbuilder抛出异常
【发布时间】:2012-01-23 20:01:08
【问题描述】:

所以,我正在尝试使用 nodelist 和 documentbuilder 在我的 Android 应用程序中解析 XML 文件。问题是,Documentbuilder.parse() 总是返回 null 并最终抛出 IOException。我猜这与文件路径错误有关。

public class EquiScoreActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProtocolFactory.GetProtocolFactory().LoadProtocol("EquiScore/assets/Protocols/LC1_2008.xml");
    }
}

public class ProtocolFactory 
{
    private static ProtocolFactory m_Instance = new ProtocolFactory();
    Document dom;

    private ProtocolFactory()
    {
    }

    public static ProtocolFactory GetProtocolFactory()
    {
        return m_Instance;
    }

    public Protocol LoadProtocol(String filename)
    {
        Protocol output = null;
        List<JudgementGroup> jGroups;
        List<GeneralAssesment> gAssesment;

        ParseXmlFile(filename);
        ParseDocument();

        return output;
    }


    private void ParseXmlFile(String filename)
    {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try 
        {
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            dom = db.parse(filename);
        }
        catch(ParserConfigurationException pce)
        {
            pce.printStackTrace();
        }
        catch(SAXException se) 
        {
            se.printStackTrace();
        }
        catch(IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }

}

基本上,我需要知道如何获取正确的文件路径或其他(也许更好?)方法来解析 XML。一种类型的每个节点可以有不同数量的子节点,这就是为什么我认为这种方法会很好。

IOException 是: “格式错误的 URL 异常” “未找到协议:EquiScore/assets/Protocols/LC1_2008.xml”

我尝试了文件名的各种变体,但似乎还是得到了这个错误。

【问题讨论】:

    标签: android xml parsing exception


    【解决方案1】:

    您收到的 IOException 是因为您传递的是一个裸路径名,而 DocumentBuilder.parse() 需要一个 URL。

    URL 的第一部分称为“协议”(它是http://www.google.com 中的http)。您的路径名未指定协议,因此错误显示为“找不到协议”。您可以通过在路径名前面加上 file:// 来创建 URL。

    或者,您可以创建一个FileFileInputStream 对象并将其传递给DocumentBuilder.parse(),因为该方法已重载。比如这样:

    dom = db.parse(new File(filename));
    

    编辑:由于您的文件位于assets/ 文件夹中,您可以使用AssetManager.open(),它返回InputStream,而DocumentBuilder.parse() 的重载版本之一又接受了它:

    AssetManager assetManager = ...
    dom = db.parse(assetManager.open("Protocols/LC1_2008.xml"));
    

    【讨论】:

    • 谢谢,这至少让我陷入了“找不到文件”的错误! :) 现在我只需要找出我的文件的路径。它位于 assets/Protocols/ 中,但这似乎不起作用。
    • 嗯,资产(assets/ 文件夹中的东西)是通过 AssetManager API 访问的,而不是文件或 URL。
    • 好的,那么最好的做法是什么?创建一个新文件夹,例如 res/XML/Protocols 并从那里获取它?编辑:好吧,我正在尝试这个,但无论如何都无法找到文件。我一定是在考虑文件路径或整个事情的错误方式?
    • 好吧,我添加了一个“要走的路”的答案。
    • @Bentebent 尝试AssetManager.list() 列出与您的应用捆绑的所有资产的路径。可能您使用的路径有问题。
    【解决方案2】:

    显然您已将 XML 放在 assets 文件夹中,因此您应该使用 AssetManager 检索 InputStream 进行解析。

    类似这样的:

    Context context = ...;
    AssetManager assManager = context.getAssets();
    DocumentBuilder db = ..;
    db.parse(assManager.open("Protocols/LC1_2008.xml"));
    

    【讨论】:

    • 太棒了!感谢您的帮助,这似乎已经解决了问题!
    猜你喜欢
    • 2016-05-24
    • 2011-05-14
    • 2021-11-23
    • 2012-02-04
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    相关资源
    最近更新 更多