Android中使用相同元素名的Android xml解析?

Android中使用相同元素名的Android xml解析?,android,xml-parsing,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>

我想解析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>
    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.
“一些数据1”
“一些数据2”
“一些数据3”
2.
“一些数据1”
“一些数据2”
“一些数据3”
有没有像这样解析xml的想法(正面)

    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());

        }



        }
编辑:

    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());

        }



        }
如果结构像这样

 <parent>
  <Title>2</Title>
  <child>"Some data1"</child>
 </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());

        }



        }

2.
“一些数据1”
我用

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

            }
    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());

        }



        }
NodeList nodes1=doc.getElementsByTagName(“父”);
对于(int i=0;i
这里我在一个对象类中存储数据,然后在数组列表中添加对象,然后在需要的地方使用数据

    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());

        }



        }
但父项中的子项名称相同时出现问题如何获取这些值。

 <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>
    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.
“一些数据1”
“一些数据2”
“一些数据3”
2.
“一些数据1”
“一些数据2”
“一些数据3”

有这方面的教程吗?

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

计数=0;
    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());

        }



        }

当元素即将结束时,只需将字符放入字符串数组并增加计数,然后在结束元素之后,如果相同的元素正在声明,则将charte设置为null并再次获取字符,然后在结束元素处将其放入数组,依此类推……

您的ciro.child应该是字符串的数组列表

    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());

        }



        }
使用下列内容

    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());

        }



        }
nodes1=doc.getElementsByTagName(“父项”)

    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());

        }



        }
for(int i=0;i对于(int j=0;j使用此代码可能会有所帮助

    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());

        }



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

            }
nodes1=doc.getElementsByTagName(“父”);
对于(int i=0;i
和类用于此

    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());

        }



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

   }
公共类函数{
公共最终静态文档XMLfromString(字符串xml){
单据单据=空;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
试一试{
DocumentBuilder db=dbf.newDocumentBuilder();
InputSource is=新的InputSource();
is.setCharacterStream(新的StringReader(xml));
doc=db.parse(is);
}捕获(ParserConfiguration异常e){
System.out.println(“XML解析错误:+e.getMessage());
返回null;
}捕获(SAXE异常){
System.out.println(“错误的XML文件结构:+e.getMessage());
返回null;
}捕获(IOE异常){
System.out.println(“I/O验证:+e.getMessage());
返回null;
}
退货单;
}
/**返回元素值
*@param elem元素(它是XML标记)
*@返回元素值,否则为空字符串
*/
公共最终静态字符串getElementValue(节点元素){
节点儿童;
if(elem!=null){
if(elem.hasChildNodes()){
for(kid=elem.getFirstChild();kid!=null;kid=kid.getNextSibling()){
if(kid.getNodeType()==Node.TEXT\u节点){
return kid.getNodeValue();
}
}
}
}
返回“”;
}
公共静态字符串getXML(){
字符串行=null;
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://p-xr.com/xml");
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
line=EntityUtils.toString(httpEntity);
}捕获(不支持的编码异常e){
line=“无法连接到服务器”;
}捕获(格式错误){
line=“无法连接到服务器”;
}捕获(IOE异常){
line=“无法连接到服务器”;
}
回流线;
}
公共静态整数结果(文档文档){
节点结果=doc.getDocumentElement();
int res=-1;
试一试{
res=Integer.valueOf(results.getAttributes().getNamedItem(“count”).getNodeValue());
}捕获(例外e){
res=-1;
}
返回res;
}
公共静态字符串getValue(元素项,字符串str){
NodeList n=item.getElementsByTagName(str);
返回XMLfunctions.getElementValue(n.item(0));
}
公共静态字符串getCharacterDataFromElement(元素e){
Node child=e.getFirstChild();
if(CharacterData的子实例){
CharacterData cd=(CharacterData)子级;
返回cd.getData();
}
返回“?”;
}
}

这可能会帮助您……

因此,您在实现SAX解析器或DOM解析器时遇到了什么问题,请澄清您的疑问,此xml中没有任何错误,通常xml记录仅以这种方式格式化。如果有人能够解决此问题:如果您想以标记级别存储数据,请忽略父标记,只需循环并搜索标记。a您需要示例代码吗?我认为XmlPullParser在这种情况下很好。但是任何解析器都可以使用。使用getC
    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());

        }



        }