在Android中使用相同的元素名称进行Android xml解析?

时间:2012-01-17 05:41:11

标签: android xml-parsing

我想解析xml文件,我有像这样的xml

<parent>
  <Title>1</Title>
  <child>"Some data1"</child>
  <child>"Some data2"</child>
  <child>"Some data3"</child>
</parent>
<parent>
  <Title>2</Title>
  <child>"Some data1"</child>
  <child>"Some data2"</child>
  <child>"Some data3"</child>
 </parent>

像这样解析xml的任何想法(UP侧)

修改

如果结构如此

 <parent>
  <Title>2</Title>
  <child>"Some data1"</child>
 </parent>

我用

  NodeList nodes1 = doc.getElementsByTagName("parent");                  

        for (int i = 0; i < nodes1.getLength(); i++) {

            Objectclass ciro = new   Objectclass ();
            Element e = (Element)nodes1.item(i);
            ciro.Title = functionsClass.getValue(e, "Title");
            ciro.child= functionsClass.getValue(e, "child");

            ArrayListClass.ItemList.add(ciro);

            }

这里我是对象类中的stroe数据,然后在数组列表中添加对象,然后在需要的地方使用数据。

但是在父母中具有相同子名称的问题如何才能获得这些值..

 <parent>
  <Title>1</Title>
 <child>"Some data1"</child>
 <child>"Some data2"</child>
 <child>"Some data3"</child>
</parent>
<parent>
 <Title>2</Title>
 <child>"Some data1"</child>
 <child>"Some data2"</child>
 <child>"Some data3"</child>
</parent>

任何教程..

5 个答案:

答案 0 :(得分:1)

你的ciro.child应该是一个字符串的数组列表。

使用以下

nodes1 = doc.getElementsByTagName(“parent”);

    for (int i = 0; i < nodes1.getLength(); i++) {

        Objectclass ciro = new   Objectclass ();
        Element e = (Element)nodes1.item(i);

        NodeList list=e.items();

        for(int j=0;j<list.getLength();j++)
        {
            Node item=list.getNode(j);
            if(item.getNodeName().equals("title"))
                ciro.title=item.getNodeValue();
            else if(item.getNodeName().equals("child")
                ciro.childs.add(item.getNodeValue());

        }



        }

答案 1 :(得分:0)

您需要使用SAXParser,请查看此链接http://www.ctctlabs.com/index.php/blog/detail/parsing_xml_on_android/

答案 2 :(得分:0)

有什么问题?我无法理解。 dom,sax,pull ...使用任何解析器都可以解析数据。只需创建一个模型,然后构造一个ArrayList并迭代标记并将数据存储到模型中。

答案 3 :(得分:0)

取一个数= 0; 元素即将结束时的时间只是将字符放入字符串数组并增加计数然后在结束元素之后如果相同的元素表示,则使charte设置为null并再次获取字符,并在end元素中将其放入数组中等等......

答案 4 :(得分:0)

使用此代码可能会有所帮助..

   nodes1 = doc.getElementsByTagName("parent");                  

        for (int i = 0; i < nodes1.getLength(); i++) {

            ObjectClass cgro = new ObjectClass();
            Element e = (Element)nodes1.item(i);
            cgro.Title = XMLfunctions.getValue(e, "Title");


            nodes1a = doc.getElementsByTagName("child");

            for(int j = 0; j < nodes1a.getLength(); j++ ){

                ObjectClass1 cgro1 = new ObjectClass1();

                 Element e2= (Element) nodes1a.item(j);
                 cgro1.child= XMLfunctions.getCharacterDataFromElement(e2);
                 ArrayListClass.ItemList1.add(cgro1);
            }


            ArrayListClass.ItemList2.add(cgro);

            }

和这个

的类使用
    public class XMLfunctions {

public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    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 "";
 }

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://p-xr.com/xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}

public static String getCharacterDataFromElement(Element e) {
      Node child = e.getFirstChild();
      if (child instanceof CharacterData) {
         CharacterData cd = (CharacterData) child;
         return cd.getData();
      }
      return "?";
    }

   }

这对你有帮助......