Sax parsing and encoding

前端 未结 3 606
醉酒成梦
醉酒成梦 2020-12-10 18:05

I have a contact that is experiencing trouble with SAX when parsing RSS and Atom files. According to him, it\'s as if text coming from the Item elements is truncated at an a

3条回答
  •  执笔经年
    2020-12-10 18:11

    The characters() method is not guaranteed to give you the complete character content of a text element in one pass - the full text may span buffer boundaries. You need to buffer the characters yourself between the start and end element events.

    e.g.

    StringBuilder builder;
    
    public void startElement(String uri, String localName, String qName, Attributes atts) {
       builder = new StringBuilder();
    }
    
    public void characters(char[] ch, int start, int length) {
       builder.append(ch,start,length);
    }
    
    public void endElement(String uri, String localName, String qName) {
      String theFullText = builder.toString();
    }
    

提交回复
热议问题