回车、换行符等空白字符也会触发characters事件,加上ignorableWhitespace()处理一下

解决方案 »

  1.   

    我自己刚好写了一个 应该content那里只是触发1次characters事件,直接读取整个字符串的内容从int start到length
    怎么会是6次的呢??
    弄代码来看看把
      

  2.   

    private class TenorNoticeHandler extends DefaultHandler {
            private java.util.Stack tags = new java.util.Stack();
            private TB_TenorNoticeBookVO tb_tenorNoticBookVO = new    TB_TenorNoticeBookVO();
            private ContentHandler parentHandler;
           
            public void setParentHandler(DefaultHandler parentHandler) {
                this.parentHandler = parentHandler;
            }        public void startElement(String uri, String localName, String tag,
                                     Attributes attribs)
                    throws SAXParseException {
                System.out.println(localName);
                tags.push(localName);
            }       /* public void ignorableWhitespace (char ch[], int start, int length)
                throws SAXException{
                System.out.println("dddddddddddddddddddddddddddddddddddddddd");
                //characters(ch, start, length);
            }  */
            /**
             *
             *
             * @param buf
             * @param start
             * @param length
             * @throws SAXParseException
             */
            public void characters(char[] buf, int start, int length)
                    throws SAXParseException {            String tag = "";
                if (!tags.isEmpty())
                   tag = (String) tags.peek();            String temp = new String(buf, start, length);
                
                System.out.println("value=================" + temp + "   length====="+temp.length());
                if (tag.equals("ID"))
                    tb_tenorNoticBookVO.setId(temp);
                else if (tag.equals("CHECKCODE"))
                    tb_tenorNoticBookVO.setCheckcode(temp);
                else if (tag.equals("CONTENT"))
                    tb_tenorNoticBookVO.setContent(temp);
                else if (tag.equals("ENDDATE")) {
                    Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp);
                    tb_tenorNoticBookVO.setEnddate(timestamp);
                }else if (tag.equals("SENDUNIT"))
                    tb_tenorNoticBookVO.setSendunit(temp);        
                else if (tag.equals("WFXW"))
                    tb_tenorNoticBookVO.setWfxw(temp);
                else if (tag.equals("UNITADMIN"))
                    tb_tenorNoticBookVO.setUnitadmin(temp);
                else if (tag.equals("ADMINSIGNDATE")){
                    Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp);
                    tb_tenorNoticBookVO.setAdminsigndate(timestamp);
                }
            }        /**
             *
             * @param uri
             * @param localName
             * @param tag
             * @throws SAXParseException
             */
            public void endElement(String uri, String localName, String tag)
                    throws SAXParseException {
                System.out.println(localName);
                startElementFlag=false;
                if (localName.equals("TENORNNOTICEBOOK")) {
                    DataOperator dataOperator = DataOperatorFactory.getTenorNotictBookInstance();
                    try {
                        //dataOperator.insertRecord(tb_tenorNoticBookVO);
                    } catch (Exception e) {
                    }
                    parser.setContentHandler(parentHandler);
                } else {
                    while (!tags.empty())
                        tags.pop();
                }
            }
        }
      

  3.   

    我是用栈来保存元素的信息的,在通过characters方法去根据当前栈的最顶元素去设置值,最后再把数据插入到数据库中,但在解析<CONTENT>1.dddd2.bbbb3.cccc</CONTENT>里面有回车件的,在这里触发了5次characters方法,每次内容是1.dddd
       回车  2.bbbb   回车  3.cccc
    这样在解析的时候就会出现问题了,
      

  4.   

    是啊,楼主能留下其他联系方式吗,
    msn [email protected]
    qq  7766284请联系我,谢谢
      

  5.   

    package core.xml;//我写的是这个 你可以参考一下 import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.helpers.*;
    import org.xml.sax.*;
    import java.util.*;
    /**
     * <p>Title: basic core</p>
     * <p>Description: zealvampire</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: zealvampire</p>
     * @author zeal
     * @version 1.0
     */public class simpleSaxParser extends DefaultHandler {
      private String name=null;
      private StringBuffer value=null;
      private HashMap hMap=null;  public simpleSaxParser() {
      }
      public void startDocument( ) throws SAXException {
        System.out.println("---SAX Event: START DOCUMENT---");
        this.hMap=new HashMap();
        this.value=new StringBuffer();
      }  public void endDocument( ) throws SAXException {
        System.out.println("---SAX Event: END DOCUMENT---");
      }  public void startElement( String namespaceURI,String localName,String qName,Attributes attr ) throws SAXException {
        System.out.println("SAX event:startElement["+localName+"]");
        if(localName.equals("param")){
          this.value.setLength(0);
          this.name = attr.getValue("name");
        }
      }  public void endElement( String namespaceURI,String localName,String qName ) throws SAXException {
        System.out.println("sax event:endElement["+localName+"]");
        if(localName.equals("param")){
          this.hMap.put(this.name,this.value.toString().trim());
          this.value.setLength(0);
        }
      }  public void characters( char[] ch, int start, int length ) throws SAXException{
        this.value.append(ch,start,length);
      }  private void printError(SAXParseException exception){
        System.out.println("\t行:\t" + exception.getLineNumber());
        System.out.println("\t列:\t" + exception.getColumnNumber());
        System.out.println("\t错误信息:\t" + exception.getMessage());
        System.out.println("-------------------");
      }
      public void warning( SAXParseException exception ) {
        System.out.println("------WARNING------");
        this.printError(exception);
      }  public void error( SAXParseException exception ) throws SAXException{
        System.out.println("------ ERROR ------");
        this.printError(exception);
      }  public void fatalError( SAXParseException exception ) throws SAXException {
        System.out.println("------ FATAL ERROR ------");
        this.printError(exception);
      }  public HashMap getResult(){
        return this.hMap;
      }
      }
      

  6.   

    <?xml version="1.0" encoding="utf-8"?>
    <parameter>
    <sys>
      <param name="INITIAL_CONTEXT_FACTORY">org.jnp.interfaces.NamingContextFactory</param>
      <param name="PROVIDER_URL">localhost:1099</param>
      <param name="URL_PKG_PREFIXES">org.jboss.naming:org.jnp.interfaces</param>
      <param name="SECURITY_PRINCIPAL">test_user</param>
      <param name="SECURITY_CREDENTIALS">test_pass</param>
      <param name="名字">1.ddd
        2.ddd3.ccc</param>
    </sys>
    </parameter>
      

  7.   

    一样的,我用你的代码测了,同样是触发了好几次,都是把1.ddd  2.ddd .....分开的