Java SaxParser在&之后修剪字符串

时间:2014-12-18 04:49:10

标签: java xml parsing

我想解析这个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_&amp;_Weight_Motorsports</uri></binding> 
  <binding name="label"><literal xml:lang="en">Davis &amp; 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 &amp; 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

正如您所看到的,它与&amp;

分开

当我通过character()函数进行追踪时,我发现长度是错误的并且达到了&amp;而不是达到我希望得到的字符串的结尾。

我复制了这部分代码并且我对解析器和处理程序知之甚少,我只知道我从跟踪代码中得到的很多,而且无论我在哪里搜索它都说应该有&amp;代替&amp;在xml文档中,就是这种情况。

我应该在这段代码中做些什么才能让完整的字符串不被&amp;字符?

1 个答案:

答案 0 :(得分:3)

这是每个人在使用SAX时必须学习的一个教训:解析器可以分解文本节点并在多次调用字符()时报告内容,并且应用程序的工作是重新组装它(例如,通过使用StringBuilder)。解析器在任何时刻打破文本是非常常见的,否则它将在内存中分流字符,例如,实体引用发生的位置或它到达I / O缓冲区边界的位置。

它的设计方式是通过最小化文本复制使SAX解析器超级高效,但我怀疑没有真正的好处,因为文本复制只需要由应用程序完成。

不要试图像@DavidWallace建议的那样对解析器进行二次猜测。允许解析器以任何方式破解文本,并且您的应用程序应该满足它。

相关问题