【发布时间】:2015-02-16 19:37:55
【问题描述】:
我要解析这个xml:
<sparql xmlns="http://www.w3.org/2005/sparql-results#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
<head>
<variable name="uri"/>
<variable name="id"/>
<variable name="label"/>
</head>
<results distinct="false" ordered="true">
<result>
<binding name="uri"><uri>http://dbpedia.org/resource/Davis_&_Weight_Motorsports</uri></binding>
<binding name="label"><literal xml:lang="en">Davis & Weight Motorsports</literal></binding>
<binding name="id"><literal datatype="http://www.w3.org/2001/XMLSchema#integer">5918444</literal></binding>
<binding name="label"><literal xml:lang="en">Davis & Weight Motorsports</literal></binding>
</result></results></sparql>
这是我的处理程序:
public class DBpediaLookupClient extends DefaultHandler{
public DBpediaLookupClient(String query) throws Exception {
this.query = query;
HttpMethod method = new GetMethod("some_uri&query=" + query2);
try {
client.executeMethod(method);
InputStream ins = method.getResponseBodyAsStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
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");
}
method.releaseConnection();
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("td") || qName.equalsIgnoreCase("uri") || qName.equalsIgnoreCase("literal")) {
tempBinding = new HashMap<String, String>();
}
lastElementName = qName;
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("uri") || qName.equalsIgnoreCase("literal") || qName.equalsIgnoreCase("td")) {
if (!variableBindings.contains(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 ("td".equals(lastElementName)) {
if (tempBinding.get("td") == null) {
tempBinding.put("td", s);
}
}
else if ("uri".equals(lastElementName)) {
if (tempBinding.get("uri") == null) {
tempBinding.put("uri", s);
}
}
else if ("literal".equals(lastElementName)) {
if (tempBinding.get("literal") == null) {
tempBinding.put("literal", s);
}
}
//if ("URI".equals(lastElementName)) tempBinding.put("URI", s);
if ("URI".equals(lastElementName) && s.indexOf("Category")==-1 && tempBinding.get("URI") == null) {
tempBinding.put("URI", s);
}
if ("Label".equals(lastElementName)) tempBinding.put("Label", s);
}
}
}
结果如下:
key: uri, value: http://dbpedia.org/resource/Davis_
key: literal, value: 5918444
key: literal, valueDavis
如您所见,它与 & 分开
当我跟踪 character() 函数时,我发现长度是错误的,并且是 & 而不是直到我想要作为结果的字符串的末尾。
我复制了这部分代码,我对解析器和处理程序知之甚少,我只知道我从跟踪代码中得到的很多东西,而且无论我在哪里搜索,都说应该有 &amp; 而不是 &在一个 xml 文档中,就是这种情况。
在这段代码中我应该怎么做才能使完整的字符串不被 & 字符修剪?
【问题讨论】:
-
请发布您正在解析的 xml 以进行一些测试。
-
我唯一能看到的可能是一个问题是某些 XML 解析器可能会为单个元素的内容提供多个
characters事件。我建议使用您的调试器,并在characters方法中设置一个断点来确定这是否正在发生。如果是这样,那么您必须让您的characters方法累积文本,直到调用endElement方法。 -
你是对的。我怎么知道是否需要累积?如果我在 startelement 和 endelement 中设置开关是否合理?
-
按照我的建议。使用您的调试器,在
characters中有一个断点,以查看它被调用了多少次,以及使用了哪些字符。这会告诉你是否需要积累。如果这不是问题,那么我不确定该建议什么;不过最好先尝试一下。 -
为什么它不起作用?如果您开始的标签是
<literal>,您应该只将空字符串放入tempBinding。如果你这样做了,那么实际的逻辑就是你现在所拥有的。