Android:解析XML时出错,IOException:读取失败:EBADF

时间:2014-02-03 08:34:48

标签: xml parsing inputstream

当我尝试将InputStream解析为Document时,我收到此错误:

java.io.IOException:读取失败:EBADF(错误的文件编号)

代码:

private String readLanguageXML(InputStream file)
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try
    {
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document dom = builder.parse(file); // ERROR HERE
        file.close();

        Element root = dom.getDocumentElement();

        NodeList items = root.getChildNodes();

        for (int j=0; j<items.getLength(); j++)
        {
            try
            {
                Node dato = items.item(j);
                String etiqueta = dato.getAttributes().item(0).getNodeValue();

                languageMap.put(etiqueta, dato.getTextContent());                  
            }
            catch(NullPointerException npe)
            {
                npe.printStackTrace();
            }
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    return "";
}

0 个答案:

没有答案