XML的JAVA 解析(一)(6b)

类别:Java 点击:0 评论:0 推荐:

class Example6Circle extends DefaultHandler {

   // 本地当前circle引用...
        private Circle   currentCircle;

   // Parent...
   ContentHandler parent;

   // XML 解析器
   XMLReader parser;

  // 从"characters" SAX事件中收集数据的缓存。
   private CharArrayWriter contents = new CharArrayWriter();

   public void collectCircle( XMLReader parser,
               ContentHandler parent,
               Circle newCircle ) {
      this.parent = parent;
      this.parser = parser;
      parser.setContentHandler( this );
      currentCircle = newCircle;
   }

  // 重载DefaultHandler类以拦截SAX事件的方法。
   //
        // 头于所有有效事件的详细内容,参见org.xml.sax.ContentHandler。
   //
   public void startElement( String namespaceURI,
               String localName,
              String qName,
              Attributes attr ) throws SAXException {
      contents.reset();
   }

   public void endElement( String namespaceURI,
               String localName,
              String qName ) throws SAXException {

      if ( localName.equals( "x" ) ) {
                          currentCircle.x =
                            Integer.valueOf
                            (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "y" ) ) {
                          currentCircle.y =
                              Integer.valueOf
                              (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "width" ) ) {
                          currentCircle.width =
                            Integer.valueOf
                            (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "height" ) ) {
                          currentCircle.height =
                             Integer.valueOf
                             (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "Circle" ) ) {
                    // 把内容处理器(content handler)换回成parent
         parser.setContentHandler(parent);
      }

   }

   public void characters( char[] ch, int start, int length )
                  throws SAXException {
      // 从"characters" SAX事件中收集数据的缓存。
      contents.write( ch, start, length );

   }

}

class Example6Square extends DefaultHandler {

   // 当前长方形本地引用...

        private Square   currentSquare;

   // Parent...
   ContentHandler parent;

   // XML 解析器
   XMLReader parser;

  // 从"characters" SAX事件中收集数据的缓存。
   private CharArrayWriter contents = new CharArrayWriter();


   public void collectSquare( XMLReader parser,
               ContentHandler parent,
               Square newSquare ) {
      this.parent = parent;
      this.parser = parser;
      parser.setContentHandler( this );
      currentSquare = newSquare;
   }


  // 重载DefaultHandler类以拦截SAX事件的方法。
   //
        // 头于所有有效事件的详细内容,参见org.xml.sax.ContentHandler。
   //
   public void startElement( String namespaceURI,
               String localName,
              String qName,
              Attributes attr ) throws SAXException {

      contents.reset();

   }

   public void endElement( String namespaceURI,
               String localName,
              String qName ) throws SAXException {

      if ( localName.equals( "x" ) ) {
                          currentSquare.x = Integer.valueOf
                             (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "y" ) ) {
                          currentSquare.y = Integer.valueOf
                             (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "width" ) ) {
                          currentSquare.width = Integer.valueOf
                             (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "height" ) ) {
                          currentSquare.height = Integer.valueOf
                             (contents.toString().trim()).intValue();
      }

      if ( localName.equals( "Square" ) ) {
                    // 把内容处理器(content handler)换回成parent
         parser.setContentHandler(parent);
      }
   }

   public void characters( char[] ch, int start, int length )
                  throws SAXException {
     // 从"characters" SAX事件中收集数据到缓存。
      contents.write( ch, start, length );

   }


}
(未完待续)

本文地址:http://com.8s8s.com/it/it18167.htm