你可以在想退出的地方抛出一个SAXException异常退出解析过程

解决方案 »

  1.   

    public class DbXmlFile extends DefaultHandler{    private Date fileCreateDate;
        private Date fileBeginDate;
        private Date fileEndDate;
        public String name;
        private XMLReader parser = new XMLReaderImpl();    public DbXmlFile(){
        }    public Date getFileCreateDate() {
            return fileCreateDate;
        }    public void setFileCreateDate(Date fileCreateDate) {
            this.fileCreateDate = fileCreateDate;
        }    public Date getFileBeginDate() {
            return fileBeginDate;
        }    public void setFileBeginDate(Date fileBeginDate) {
            this.fileBeginDate = fileBeginDate;
        }    public Date getFileEndDate() {
            return fileEndDate;
        }    public void setFileEndDate(Date fileEndDate) {
            this.fileEndDate = fileEndDate;
        }    public void startElement(String uri, String localName, String tag,
                                     Attributes attribs)
                    throws SAXParseException {
                if (localName.equals("ROOT")) {
                    String beginDate = attribs.getValue("beginDate");
                    String endDate = attribs.getValue("endDate"); 
                    this.fileBeginDate=Date.valueOf(beginDate );
                    this.fileEndDate=Date.valueOf(endDate);
                    System.out.println("date=============="+fileBeginDate);//这里已经set过了,但最后取不到啊
                    //throw new SAXParseException("sss",new LocatorImpl());
                }
            }    public String parse(File xmlFile) throws FileNotFoundException, IOException,Exception
        {        Reader in = new BufferedReader(new InputStreamReader(new
                     FileInputStream(xmlFile), "UTF8"));
            try {
                parser.setContentHandler(new DbXmlFile());
                parser.parse(new InputSource(in));
            } catch (SAXException saxe) {
                if (saxe instanceof SAXParseException) {
                    SAXParseException saxpe = (SAXParseException) saxe;
                    int line = saxpe.getLineNumber();
                    int col = saxpe.getColumnNumber();
                    String publicID = saxpe.getPublicId();
                    String message = "XML parsing exception (" + publicID +
                            ") line " + line + ":" + col;
                    throw new Exception(message);
                }
            }
            return this.fileBeginDate.toString();
        }    public static void main(String[] args) throws Exception
        {
            DbXmlFile dbXmlFile = new DbXmlFile();
            File file = new File("d:\\project\\xfk\\db\\date\\20040916134141601.xml");
            System.out.println(dbXmlFile.parse(file));
            System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢,在程序里面这个值已经设过了,怎么没取道啊
        }
    }最后取出的beginDate都是null,为什么啊
      

  2.   

    确定解析过程是在throw new SAXParseException("sss",new LocatorImpl());处退出的吗?没有这句能得到正确的结果吗?
      

  3.   

    程序没有异常,况且也执行到System.out.println("date=============="+fileBeginDate);
    这边了,也打印出来了,但在最后System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢
    在外面就打取不到啊,
      

  4.   

    抛异常是个很不好的创意,你要知道抛出异常以后:程序的运行会比正常情况下慢2-3个数量级。
    如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
      

  5.   

    你的fileEndDate到最后读出来的时候是null,我也遇到过这个问题,我是把变量变为static类型的才搞定,也没弄清楚是什么原因造成的
      

  6.   

    xkak2(矗立云端) :
      如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
    你是说用dom来解析吗,但xml很大时,不是要全部加载进去,这样不是很耗内存吗
      

  7.   

    为什么要用sax、dom呢?你只是要根的属性,自己写个xml的分析函数,xml的声明之后的第一个“<”“>”之间的难道不是根吗?