我想解析 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);
}
这里我在一个对象类中存储数据,然后在数组列表中添加对象,然后在我需要的地方使用数据。
但是如果父项中有相同的子名称,我该如何获取这些值..
<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>
关于这个的任何教程..
最佳答案
您的 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());
}
}
关于在android中使用相同的元素名称解析Android xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8890206/