Having trouble saving node input to a string
Below is a class I have for a 2 class project. Basically I want to parse
the title of the wiki page, and save it to the string title or something I
can call from another class using something like
retrieveTitle.setText(WikiSearcherExtension.title); In eclipse its telling
me the local variable title isnt being used at all to store the node
information.
It wouldnt let me paste the xml as a block of code so here is the url I
have been using
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=xml&rvprop=timestamp&rvlimit=1&rvtoken=rollback&titles=test&redirects=
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();
}
}
}
}
No comments:
Post a Comment