0
我無法弄清楚爲什麼會給出錯誤的值。如何在XMLTree中檢查並返回
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
boolean x = true;
out.print("Enter a URL or file name for an XML source: ");
String url = in.nextLine();
XMLTree xml = new XMLTree2(url);
out.print("Enter the name of a tag: ");
String tag = in.nextLine();
out.println(findTag(xml, tag));
}
/**
* Reports whether the given tag appears in the given {@code XMLTree}.
*
* @param xml
* the {@code XMLTree}
* @param tag
* the tag name
* @return true if the given tag appears in the given {@code XMLTree}, false
* otherwise
* @ensures <pre>
* findTag =
* [true if the given tag appears in the given {@code XMLTree}, false otherwise]
* </pre>
*/
private static boolean findTag(XMLTree xml, String tag) {
boolean result = false;
if (xml.isTag()) {
for (int i = 0; i < xml.numberOfChildren(); i++) {
findTag(xml.child(i), tag);
System.out.println("label is " + xml.label());
if (xml.label().equals(tag)) {
result = true;
System.out.println(result);
}
}
}
return result;
}
}
給出XML樹的代碼運行,以便發現所有存在於樹中的標籤的時候。它將每個標籤與正在搜索的標籤進行比較。我該如何做到這一點,以便在XML中找到給定標籤時,它會更新布爾變量並停止搜索更多。