dom xml parsering problem
hi all i have a xml file and 开发者_如何转开发matching with a string value for each element's getTextContent() or getNodeValue(). but the if condition not matching even though the string value is in the xml
String xmlvalue=""; xmlvalue=String.valueOf(section1.item(j).getChildNodes().item(k).getTextContent()); if (xmlvalue=="A"){ System.out.println("jjj"); } i tried following also
String xmlvalue="";
xmlvalue=section1.item(j).getChildNodes().item(k).getTextContent();
if (xmlvalue=="A"){
System.out.println("jjj");
}
if condition does not work please help me. but it print all element is in xml. if condition only not working.
If you want to compare something's value use .equals >
xmlvalue.equals("A")
xmlvalue == "A" will check if the two are the same object (which they are clearly not).
And I think writing "A".equals(xmlvalue)
is preffered to avoid nullpointer in case xmlvalue is null.
The best practise, of course, is to have your "A" in some final field
private final static String A = "A"
and compare as follows:
A.equals(xmlvalue)
And the best of all would be using xmlPullParser, since DOM loads the whole xml in memory, which might be critical on such hardware limited devices such as a mobile phone.
精彩评论