【问题标题】:How to parse an XML file in an Android app如何在 Android 应用程序中解析 XML 文件
【发布时间】:2011-03-30 08:30:49
【问题描述】:

我正在尝试解析如下的 XML 文件

<Subject>
    <chapter>
       <Question>abc</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
        <Question>def</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
         <Question>ccsv</Question>
        <answer>avksn</answer>
    </chapter>
</Subject>

在这我可以计算章节的数量。章节数等于问答数。我还在布局中放置了一个名为 ok 的按钮。

现在我想显示第一个问题,点击确定后我想显示第二个问题,直到最后。当我到达最后一个问题时,我想转移到一个新的活动。

如何执行此操作,请帮助我

【问题讨论】:

    标签: android xml-parsing saxparser


    【解决方案1】:

    将 xml 读入 InputStream,然后:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    doc = db.parse([the InpuTstream]);
    

    然后你可以使用像这样的文档:

      if(doc.getElementsByTagName("GeometryCollection").getLength()>0){
        org.w3c.dom.Node parent_node = doc.getElementsByTagName("GeometryCollection").item(0);
        NodeList nl = parent_node.getChildNodes();
        for(int i = 0;i<nl.getLength();i++){
         ...
    

    【讨论】:

      【解决方案2】:

      将 XML 读入文档(v = xml 字符串)

      public Document XMLfromString(){
      
          Document doc = null;
      
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          try {
      
              DocumentBuilder db = dbf.newDocumentBuilder();
      
              InputSource is = new InputSource();
              is.setCharacterStream(new StringReader(v));
              doc = db.parse(is); 
      
          } catch (ParserConfigurationException e) {
              e.printStackTrace();
          } catch (SAXException e) {
              System.out.println("Wrong XML file structure: " + e.getMessage());
              return null;
          } catch (IOException e) {
              e.printStackTrace();
          }
      
          return doc;
      
      }
      

      然后像这样获取元素:

      /** Returns element value
        * @param elem element (it is XML tag)
        * @return Element value otherwise empty String
        */
       public final static String getElementValue( Node elem ) {
           Node kid;
           if( elem != null){
               if (elem.hasChildNodes()){
                   for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                       if( kid.getNodeType() == Node.TEXT_NODE  ){
                           return kid.getNodeValue();
                       }
                   }
               }
           }
           return "";
       }
      

      使用方法:

      Document doc = x.XMLfromString();
      NodeList nodes = doc.getElementsByTagName("result");
      
          for (int i = 0; i < nodes.getLength(); i++) {                           
              HashMap<String, String> map = new HashMap<String, String>();    
      
              Element e = (Element)nodes.item(i);
              map.put("id", x.getValue(e, "orgid"));
              map.put("bedrijf", x.getValue(e, "naam"));
              map.put("plaats", x.getValue(e, "plaats"));
              mylist.add(map);            
          }
      

      【讨论】:

        猜你喜欢
        • 2011-08-28
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        相关资源
        最近更新 更多