我正在为一个朋友写一个小项目。记事卡应用程序。我的计划是将记事卡放在 xml 格式中,以便我可以轻松地导入和导出它们。我在 assets/xml/mynotecard.xml 文件夹中找到了 xml 文件,但我无法访问该文件。
每当我尝试解释 xml 文件(稍后将放在课堂上)时,我都会遇到异常:test.xml 不是文件。这是我的代码的摘录:
公共类 NotecardProActivity 扩展 Activity {
List<String> xmlFiles;
public ArrayList<File> xmlFileList;
XMLInterpreter horst;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AssetManager mgr = getAssets();
displayFiles(mgr, "",0);
xmlFiles = displayFiles(mgr, "xml",0);
for (int e = 0; e<=xmlFiles.size()-1;e++)
{
Log.v("Inhalt List"+e+": ", xmlFiles.get(e));
}
xmlFileList = new ArrayList<File>();
for (int i = 0; i<=xmlFiles.size()-1;i++)
{
xmlFileList.add(new File("./assets/xml/"+xmlFiles.get(i)));
}
for (int i = 0; i<=xmlFileList.size()-1;i++)
{
Log.v("Name: ", xmlFileList.get(i).getName());
Log.v("Filelocation: ",xmlFileList.get(i).getPath());
Log.v("Filelocation: ",xmlFileList.get(i).getAbsolutePath());
}
Log.v("DEBUG","XML FILE LISTE erfolgreich geschrieben!");
//Alternative zum ausgelagerten XML INTERPRETER
try
{
if(xmlFileList.get(0) == null){
Log.v("Debug", "XMLFILE IS NULL");
}
else{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFileList.get(0));
doc.getDocumentElement().normalize();
Log.v("Root element :", doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("notecard");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Log.v("GesetzesText: " , getTagValue("legal_text", eElement));
// System.out.println("Kommentar : " + getTagValue("comment", eElement));
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
mgr.close();
}
private List<String> displayFiles (AssetManager mgr, String path, int level) {
List<String> abc = new ArrayList<String>();
Log.v("Hello","enter displayFiles("+path+")");
try {
String list[] = mgr.list(path);
abc.addAll(Arrays.asList(list));
Log.v("Hello1","L"+level+": list:"+ Arrays.asList(list));
} catch (IOException e) {
Log.v("Fail","List error: can't list" + path);
}
return abc;
}
private String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
如果你们能帮助我,那就太棒了:)