【发布时间】:2016-04-09 09:11:08
【问题描述】:
只想从我在微调器中获取的 XML 中获取唯一值....我能够在微调器中获取值,但所有重复值也显示在微调器下拉列表中。
例如我的xml是
<a:AAAA>
<a:Q>123</a:Q>
<a:W>yessssssss</a:W> <--- THIS VALUE IS SAME
<a:E>275</a:E> ^
<a:R>wwwwqwq</a:R> |
</a:AAAA>
So want only single value in andrid spinner how to achive this?
<a:AAAA> |
<a:Q>456</a:Q> v
<a:W>yessssssss</a:W> <--- AND THIS VALUE IS SAME
<a:E>648</a:E>
<a:R>qwqwqsd</a:R>
</a:AAAA>
<a:AAAA>
<a:Q>789</a:Q>
<a:W>Hiiii</a:W>
<a:E>269</a:E>
<a:R>ds</a:R>
</a:AAAA>
<a:AAAA>
<a:Q>867</a:Q>
<a:W>qwqwqw</a:W>
<a:E>1648</a:E>
<a:R>wqw</a:R>
</a:AAAA>
以下代码显示微调器中的值,但数据重复
public void onNothingSelected(AdapterView<?> arg0) {
}
protected void parse() {
// TODO Auto-generated method stub
try {
URL url = new URL(
"web");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("a:AAAA");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("a:Q");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
NodeList websiteList = fstElmnt.getElementsByTagName("a:W");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
NodeList websiteList1 = fstElmnt.getElementsByTagName("a:E");
Element websiteElement1 = (Element) websiteList1.item(0);
websiteList1 = websiteElement1.getChildNodes();
NodeList websiteList2 = fstElmnt.getElementsByTagName("a:R");
Element websiteElement2 = (Element) websiteList2.item(0);
websiteList2 = websiteElement2.getChildNodes();
title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());
}
已编辑-
public void onNothingSelected(AdapterView<?> arg0) {
}
protected void parse() {
// TODO Auto-generated method stub
try {
URL url = new URL(
"web");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("a:AAAA");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("a:Q");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
NodeList websiteList = fstElmnt.getElementsByTagName("a:W");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
NodeList websiteList1 = fstElmnt.getElementsByTagName("a:E");
Element websiteElement1 = (Element) websiteList1.item(0);
websiteList1 = websiteElement1.getChildNodes();
NodeList websiteList2 = fstElmnt.getElementsByTagName("a:R");
Element websiteElement2 = (Element) websiteList2.item(0);
websiteList2 = websiteElement2.getChildNodes();
title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());
Set<String> uniqueTitles = new HashSet<String>(title);
title3 = new ArrayList<String>(uniqueTitles);
}
【问题讨论】:
标签: java android xml xml-parsing