我试图从树中读取一个名为“版本”的属性的值。其值为“2.0”。我设置了一个 if 语句,询问属性值是否等于 2.0 来运行此代码,而不是运行 else 语句。我很困惑我做了一个布尔值并将其设置为等于等式。我把它打印出来,当它确实是真的时它读作假。这是我的代码:
out.print("Enter the URL of an RSS 2.0 news feed: ");
String url = in.nextLine();
XMLTree xml = new XMLTree1(url);
boolean t = xml.hasAttribute("version");
if (t) {
out.println(xml.attributeValue("version"));//this prints 2.0
boolean a = (xml.attributeValue("version") == "2.0"); //added this to debugg
out.println(a); //this gets set to false. why?
if (xml.attributeValue("version") == "2.0") {
out.println("Hello");
} else {
out.println("URL entered is not of version 2.0");
}
} else {
out.println("No attribute Version");
}
/*
* TODO: fill in body
*/
in.close();
out.close();
}
}
我输入了 URL: http: //news.yahoo.com/rss/,它的树有一个根标签“rss”,它的属性“version”等于 2.0: