下面是我为 2 类项目开设的课程。基本上我想解析 wiki 页面的标题,并将其保存到字符串标题或我可以使用类似retrieveTitle.setText(WikiSearcherExtension.title);
In eclipse 之类的东西从另一个类调用的东西,它告诉我根本没有使用局部变量标题来存储节点信息。
它不会让我将 xml 粘贴为代码块,所以这是我一直在使用的 url http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=xml&rvprop=timestamp&rvlimit=1&rvtoken=rollback&titles =测试&重定向=
public class WikiParserTrials {
private String wikiInformation;
private String wikiUrl;
String title;
public void urlRefactor(String url) throws IOException {
String wikiPageName = url.replaceAll(" ", "_");
wikiUrl = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=xml&rvprop=timestamp&rvlimit=1&rvtoken=rollback&titles=test&redirects=";
setUrlInformation();
}
private void setUrlInformation() throws IOException {
URL url = new URL(wikiUrl);
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
wikiInformation = "";
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
wikiInformation += line;
}
}
public class ReadAndPrintXMLFile {
public void main(String argv[]) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(wikiInformation));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("normalized");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList name = element.getElementsByTagName("to");
Element line = (Element) name.item(0);
String title = (getCharacterDataFromElement(line));
}
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
}