0
我有一个带有标签国家携带名称,alpha2和拨号代码的xml文件如何匹配NodeList中的特定项目并获取它NodeValues?
现在我有一个字符串值(这是一个拨号代码),我想检查它是否存在于diallingcode标记,如果它它应该返回国家和阿尔法的名称2. 我已经做了一些代码,但一直在我有*的if语句出错。 请帮我解决一下这个问题吗? 这里是xml文件
<countries>
<country name="AFGHANISTAN" alpha2="AF" diallingcode="93"/>
<country name="ÅLAND ISLANDS" alpha2="AX"/>
<country name="ALBANIA" alpha2="AL" diallingcode="355"/>
<country name="ALDERNEY"/>
<country name="ALGERIA (El Djazaïr)" alpha2="DZ" diallingcode="213"/>
<country name="AMERICAN SAMOA" alpha2="AS" diallingcode="1-684"/>
这里是我的课堂活动
public class NewTester extends Activity {
/** Called when the activity is first created. */
TextView display;
String getinput;
String data,result;
int code;
InputStream instream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display=(TextView)findViewById(R.id.tvdisplay);
String dphoneNumber="9345678";
data=dphoneNumber.substring(0,2);
try {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
instream = getResources().openRawResource(R.raw.mycountries);
Document doc=db.parse(instream,null);
doc.getDocumentElement().normalize();
NodeList nodeList=doc.getElementsByTagName("country");
for(int i=0;i<nodeList.getLength();i++){
Element ele=(Element)nodeList.item(i);
NodeList Countryname=ele.getElementsByTagName("name");
NodeList CountryAlpha=ele.getElementsByTagName("alpha2");
NodeList Countrydial=ele.getElementsByTagName("diallingcode");
if(Countrydial.item(0).getNodeValue()==data){//I get error when I run the application from here.
result=CountryAlpha.item(0).getNodeValue();
}
instream.close();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
display.setText(""+result);
}
}
感谢Milllllllllllllllllll。你救了我。一直在头痛分拣这件事。 – 2012-03-16 15:08:31