Hi!
I'm trying to use the Lookup web service from DBpedia, but I'm finding
difficulties because there's very few information on the web. I found a
code example, but when I execute it, *I get the errors:
*java.lang.NullPointerException at
tutorial.DBpediaLookupClient.containsSearchTerms(DBpediaLookupClient.java:111)
at tutorial.DBpediaLookupClient.endElement(DBpediaLookupClient.java:87) at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
Source) at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source) at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at
org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at
org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source) at
javax.xml.parsers.SAXParser.parse(SAXParser.java:195) at
tutorial.DBpediaLookupClient.(DBpediaLookupClient.java:56) at
tutorial.DBpediaLookupClient.main(DBpediaLookupClient.java:126)
*HERE IS THE CODE:
*
package tutorial;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import com.sun.org.apache.bcel.internal.classfile.Attribute;
public class DBpediaLookupClient extends DefaultHandler {
/**
* @param args
*/
private List<Map<String, String>> variableBindings = new
ArrayList<Map<String, String>>();
private Map<String, String> tempBinding = null;
private String lastElementName = null;
private String query = "";
public DBpediaLookupClient( String query ) throws Exception {
this.query = query;
HttpClient client = new HttpClient();
HttpMethod method = new
GetMethod("http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&QueryString="+query);
try{
client.executeMethod(method);
System.out.println(method);
InputStream ins = method.getResponseBodyAsStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
javax.xml.parsers.SAXParser sax = factory.newSAXParser();
sax.parse(ins, this);
}catch (HttpException he) {
System.err.println("Http error connecting to
lookup.dbpedia.org");
}catch (IOException ioe) {
System.err.println("Unable to connect to lookup.dbpedia.org");
}catch (ParserConfigurationException e) {
System.out.println("O parser não foi configurado
corretamente.");
e.printStackTrace();
}catch (SAXException e) {
System.out.println("Problema ao fazer o parse do arquivo.");
e.printStackTrace();
}
method.releaseConnection();
}
public void startElement(String uri, String localName, String
qName, Attribute attributes) throws SAXException {
if (qName.equalsIgnoreCase("result")) {
tempBinding = new HashMap<String, String>();
}
lastElementName = qName;
}
public void endElement(String uri, String localName, String
qName) throws SAXException {
if (qName.equalsIgnoreCase("result")) {
System.out.println("Qname:" + qName);
if (!variableBindings.contains(tempBinding) &&
containsSearchTerms(tempBinding))
variableBindings.add(tempBinding);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
String s = new String(ch, start, length).trim();
if (s.length() > 0) {
if ("Description".equals(lastElementName))
tempBinding.put("Description", s);
if ("URI".equals(lastElementName)) tempBinding.put("URI", s);
if ("Label".equals(lastElementName)) tempBinding.put("Label", s);
}
}
public List<Map<String, String>> variableBindings() {
return variableBindings;
}
private boolean containsSearchTerms(Map<String, String> bindings) {
StringBuilder sb = new StringBuilder();
for (String value : bindings.values()){
sb.append(value); // do not need white space
}
String text = sb.toString().toLowerCase();
StringTokenizer st = new StringTokenizer(this.query);
while (st.hasMoreTokens()) {
if (text.indexOf(st.nextToken().toLowerCase()) == -1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
try {
DBpediaLookupClient client = new DBpediaLookupClient("berlin");
} catch (Exception e) {
e.printStackTrace();
}
}
}
*If anybody has any idea of what's wrong please help me.
Or if any of you has any other code example it will help a lot
too.*
*Thank you!*
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Dbpedia-discussion mailing list
Dbpedia-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dbpedia-discussion