Android的XML解析与Android中相同的元素名称? [英] Android xml parsing with same element name in android?

查看:92
本文介绍了Android的XML解析与Android中相同的元素名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析XML文件我有这样的XML

i want to parse xml file i have xml like this

<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侧)

any idea to parse xml like this(UP side)

编辑:

如果是这样的结构

 <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的数据,然后添加在数组列表对象,然后使用数据,我需要。

here i am stroe data in a object class and then add object in array list then use data where i need.

但是问题在父母我怎样才能得到这些值同一个孩子的名字。

 <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>

任何教程这个..

any tutorial for this..

推荐答案

使用code可这将有助于..

use this code may this will help..

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

   }

这可能会帮助你...

may this help you...

这篇关于Android的XML解析与Android中相同的元素名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆