Sax parser read a line not totally

旧城冷巷雨未停 提交于 2019-12-13 00:09:02

问题


I'm trying to parse a simil-InkML document. Every content's node has more tuple (separated by comma) with 6 or 7 number (negative and decimal too).

In testing I see that the method character of SAX don't memorize all the data.

The code:

    public class PenParser extends DefaultHandler {

   //code useless

public void characters(char ch[], int start, int length) throws SAXException {
//begin my debug print
        StringBuilder  buffer=new StringBuilder ();
        for(int i=start;i<length;i++){
            buffer.append(ch[i]);
        }
        System.out.println(">"+buffer);
//end my debug print

In debug, I see that buffer don't contain all the number of the interested tag, but it contain only the first 107 (more or less) char of content of the tag (my rows are not longer that 4610 char): it's strange this cut of char by StringBuffer and SAX parsing, in my opinion.

I had used the StringBuilder too but the problem remain.

Any suggest?


回答1:


Yes - that's pretty obvious. characters may be called several times when one node is parsed.

You'll have to use the StringBuilder as member, append the content in characters and deal with the content in endElement.

edited

btw. you do not need to build the buffer character by character - this is my implementation of characters (which I always use)

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        characters.append(new String(ch,start,length));
    }

... and not to forget ....

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException 
    {
                    final String content = characters.toString().trim();
                    // .... deal with content
                    // reset characters
        characters.setLength(0);
    }

 private final StringBuilder characters = new StringBuilder(64);


来源:https://stackoverflow.com/questions/14850076/sax-parser-read-a-line-not-totally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!