我正在尝试使用来自 DBpedia 的 Lookup Web 服务,但我遇到了困难,因为网络上的信息很少。我找到了一个代码示例,但是当我执行它时,我得到了错误:
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.<init>(DBpediaLookupClient.java:56)
at tutorial.DBpediaLookupClient.main(DBpediaLookupClient.java:126)
这是代码:
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();
}
}
}
如果有人知道出了什么问题,请帮助我。或者,如果你们中的任何人有任何其他代码示例,它也会有很大帮助。