0

尝试使用以下代码解析 res/xml 中的 xml 文件。但是当启动项目时,它显示异常“没有这样的文件/目录”。代码中是否有任何错误?立即给我一个答案。在此先感谢。

 try
    {
        DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
        *Document doc=docBuilder.parse(new File("project_data.xml"));*
        NodeList projects=doc.getElementsByTagName("PROJECTDESC");
        for(int i=0;i<projects.getLength();i++)
        {
            Element project=(Element)projects.item(i);
            ProjectData projectdata=new ProjectData(null, null, null, null, null, null);

            projectdatas.add(projectdata);
            projectdata.setProject(project.getFirstChild().getNodeValue());
            projectdata.getProject();
        }
    }
catch (Throwable t) {
        Toast.makeText(this, "Request failed: "+t.toString(), 4000).show();
    }

XML 文件是

<?xml version="1.0" encoding="ISO-8859-1"?>
<Results>
    <Row>
        <PROJECTID>1000</PROJECTID>
        <PROJECTDESC>ERP Tool</PROJECTDESC>
        <MANAGER>John Jakes</MANAGER>
        <SPONSOR>Gregory Mallor</SPONSOR>
        <STARTDATE>02-01-2010</STARTDATE>
        <PLANNEDCOMPDATE>06-21-2010</PLANNEDCOMPDATE>
        <PROBABLECOMPDATE>06-30-2010</PROBABLECOMPDATE>
        <STATUS>Active</STATUS>
        <PLNRSK>59.5065</PLNRSK>
        <EPLRSK/>
        <EPRRSK/>
        <BDTRSK/>
        <ACTUALCOMPDATE/>
        <MILESTONETOLERANCE>2</MILESTONETOLERANCE>
        <ACTIVITYTOLERANCE>1</ACTIVITYTOLERANCE>
        <WORKFLOWID>10002</WORKFLOWID>
        <CREATED>02/10/2010 16:20:43 PM</CREATED>
    </Row>
    <Row>
        <PROJECTID>1001</PROJECTID>
        <PROJECTDESC>CMS Implementation</PROJECTDESC>
        <MANAGER>Florence Marie</MANAGER>
        <SPONSOR>Gregory Mallor</SPONSOR>
        <STARTDATE>02-11-2010</STARTDATE>
        <PLANNEDCOMPDATE>02-16-2010</PLANNEDCOMPDATE>
        <PROBABLECOMPDATE>02-17-2010</PROBABLECOMPDATE>
        <STATUS>Open</STATUS>
        <PLNRSK/>
        <EPLRSK/>
        <EPRRSK/>
        <BDTRSK/>
        <ACTUALCOMPDATE/>
        <MILESTONETOLERANCE>1</MILESTONETOLERANCE>
        <ACTIVITYTOLERANCE>1</ACTIVITYTOLERANCE>
        <WORKFLOWID>10004</WORKFLOWID>
        <CREATED>02/10/2010 17:07:14 PM</CREATED>
    </Row>
    <Row>
        <PROJECTID>1002</PROJECTID>
        <PROJECTDESC>Key Account Manager Recruiting</PROJECTDESC>
        <MANAGER>Florence Marie</MANAGER>
        <SPONSOR>John Jakes</SPONSOR>
        <STARTDATE>02-11-2010</STARTDATE>
        <PLANNEDCOMPDATE>02-13-2010</PLANNEDCOMPDATE>
        <PROBABLECOMPDATE>02-13-2010</PROBABLECOMPDATE>
        <STATUS>Open</STATUS>
        <PLNRSK/>
        <EPLRSK/>
        <EPRRSK/>
        <BDTRSK/>
        <ACTUALCOMPDATE/>
        <MILESTONETOLERANCE>1</MILESTONETOLERANCE>
        <ACTIVITYTOLERANCE>1</ACTIVITYTOLERANCE>
        <WORKFLOWID>10009</WORKFLOWID>
        <CREATED>02/10/2010 17:07:18 PM</CREATED>
    </Row>
    <Row>
        <PROJECTID>1003</PROJECTID>
        <PROJECTDESC>Fast Food Regional Market Analysis</PROJECTDESC>
        <MANAGER>John Jakes</MANAGER>
        <SPONSOR>Florence Marie</SPONSOR>
        <STARTDATE>02-12-2010</STARTDATE>
        <PLANNEDCOMPDATE>02-16-2010</PLANNEDCOMPDATE>
        <PROBABLECOMPDATE>02-16-2010</PROBABLECOMPDATE>
        <STATUS>Open</STATUS>
        <PLNRSK/>
        <EPLRSK/>
        <EPRRSK/>
        <BDTRSK/>
        <ACTUALCOMPDATE/>
        <MILESTONETOLERANCE>2</MILESTONETOLERANCE>
        <ACTIVITYTOLERANCE>1</ACTIVITYTOLERANCE>
        <WORKFLOWID>10010</WORKFLOWID>
        <CREATED>02/10/2010 17:07:22 PM</CREATED>
    </Row>
    <Row>
        <PROJECTID>1004</PROJECTID>
        <PROJECTDESC>ss</PROJECTDESC>
        <MANAGER>John Jakes</MANAGER>
        <SPONSOR>Gregory Mallor</SPONSOR>
        <STARTDATE>02-11-2010</STARTDATE>
        <PLANNEDCOMPDATE>02-16-2010</PLANNEDCOMPDATE>
        <PROBABLECOMPDATE>02-17-2010</PROBABLECOMPDATE>
        <STATUS>Open</STATUS>
        <PLNRSK/>
        <EPLRSK/>
        <EPRRSK/>
        <BDTRSK/>
        <ACTUALCOMPDATE/>
        <MILESTONETOLERANCE>1</MILESTONETOLERANCE>
        <ACTIVITYTOLERANCE>1</ACTIVITYTOLERANCE>
        <WORKFLOWID>10080</WORKFLOWID>
        <CREATED>06/27/2010 19:16:26 PM</CREATED>
    </Row>
</Results>
4

2 回答 2

1

如果它给您的异常是“没有这样的文件/目录”,那么您的错误在于project_data.xml文件的位置/名称。查看资源以获取有关 Android 中文件存储的更多信息……您的 xml 文件可能不在您期望的位置。

于 2010-07-02T05:34:10.297 回答
1

创建您的res\raw xml 文件,然后使用:

Resources res = getResources();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

InputStream is = res.openRawResource(R.raw.file);

Document doc=db.parse(new InputSource(is));
于 2011-08-11T06:15:31.490 回答