Java SaxParser truncates string after & amp;

I want to parse this 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>

This is my handler:

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);
    }
  }
}

And this is the result:

key: uri, value: http://dbpedia.org/resource/Davis_
key: literal, value: 5918444
key: literal, valueDavis

As you can see, it is separated from &

When I go through the character () function, I see that the length is wrong and depends on it and does not reach the end of the line that I want to get as a result.

I copied this part of the code, and I know little about the parser and handlers, I just know that a lot of what I got from code tracking, and everywhere I looked, said what should be &amp;instead in the XML document, which takes place here.

What should I do in this code to get the full string without receiving it and the character?

+4
source share
1 answer

, SAX: (), (, StringBuilder). , , . /.

, SAX , , , , .

, @DavidWallace. , , .

+3

Source: https://habr.com/ru/post/1568085/


All Articles