I am currently working on an Android app and that means learning Java. I've toyed around with Python for a few years, but decided it was time to step up now that I have an android phone. The app basically displays a list of video games stored in an XML file locally. Right now, the structure of the XML file is basically games>game (Multiple)>name (Plus other things not important now). I am currently trying to get a list of the names of the games. I've looked up tutorials and info but none of it seems to be quite what I need. I want to actually understand how it works, not just have a working piece of code I can copy/paste. Also, keep in mind that the list of names has to end up as an array of strings for Android to use it. Here's the function I have right now (Copy/pasted from a tutorial and heavily edited, so it's not readable. I'll fix that once it's actually working.) Right now the listview shows up as empty. At least it's better than before and it doesn't crash anymore though...

我目前正在开发Android应用程序,这意味着学习Java。我已经玩弄了几年Python,但现在我决定加入Android手机了。该应用程序基本上显示本地存储在XML文件中的视频游戏列表。现在,XML文件的结构基本上是游戏>游戏(多个)>名称(加上现在不重要的其他东西)。我目前正在尝试获取游戏名称列表。我查阅了教程和信息,但似乎没有一个我需要的东西。我想真正理解它是如何工作的,而不仅仅是我可以复制/粘贴一段代码。此外,请记住,名称列表必须最终作为Android的字符串数组才能使用它。这是我现在的功能(从教程中复制/粘贴并经过大量编辑,所以它不可读。一旦它实际工作,我会修复它。)现在listview显示为空。至少它比以前更好,它不会再崩溃了......

public static String[] parse(String filename) {
      ArrayList<String> gamesList = new ArrayList<String>();

      try {
      File file = new File(filename);
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      doc.getDocumentElement().normalize();
      NodeList nodeList = doc.getElementsByTagName("game");

      for (int s = 0; s < nodeList.getLength(); s++) {

        Node fstNode = nodeList.item(s);

        //if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

          Element name = (Element) fstNode;
               Element fstElmnt = (Element) fstNode;
          NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("name");
          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          NodeList fstNm = fstNmElmnt.getChildNodes();

          gamesList.add(fstNmElmnt.toString());
        //}

      }
      } catch (Exception e) {
        e.printStackTrace();
      }
    String[] gamesArray;
    gamesArray = (String[]) gamesList.toArray(new String[0]);
    return gamesArray;
     }

3 个解决方案

#1


1

In your code, at the point that you add fstNmElmnt.toString() to gameList, it is the Element corresponding to the tag of the game. Assuming that your XML is structured <name>Joe</name>, then you need to get the value of the first child (instead of calling toString() for the Element itself):

在您的代码中,在向游戏列表添加fstNmElmnt.toString()时,它是与游戏标记相对应的元素。假设您的XML是结构化的 Joe ,那么您需要获取第一个子节点的值(而不是为Element本身调用toString()):

gamesList.add(fstNmElmnt.getFirstChild().getNodeValue());

By the way, unless you have <name> tags in other parts of your document, or need the <game> element for other processing at this stage, you can use the following (much simpler) code:

顺便说一句,除非你有 <名> 在文档的其他部分的标签,或需要 <游戏> 元素其他处理在这个阶段,你可以使用下面的(更简单)的代码:

NodeList nodeList = doc.getElementsByTagName("name");
for (int s = 0; s < nodeList.getLength(); ++s) {
    gamesList.add(nodeList.item(s).getFirstChild().getNodeValue());
}

更多相关文章

  1. android 2D 游戏的开发的方法
  2. Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表
  3. 在android上滚动时,列表视图的位置会发生变化
  4. android 获取正在运行的应用程序列表
  5. 自定义BaseAdapter,在主Activity页面调用显示歌曲列表
  6. 纯Java开发的游戏引擎V0.5--DEMO2 -- 物理引擎
  7. javascript实现设置select下拉列表框中选中内容。
  8. Java简单游戏开发之碰撞检测
  9. android游戏开发学习 博客分类: android

随机推荐

  1. php中=、==和===的区别介绍
  2. API常用签名验证方法(PHP实现)
  3. php中&&的含义及用法介绍
  4. php中$this的用法介绍
  5. 了解PHP中self关键字的相关知识
  6. 使用PHP求最大奇约数的和
  7. PHP代码优化的53个细节和优化策略
  8. 浅谈PHP面向对象的继承
  9. 谈谈关于PHP内存溢出的思考
  10. PHP实现根据出生年月日计算年龄的功能(代