Save This Page
Home » dom4j-1.6.1 » org.dom4j.samples.dom » [javadoc | source]
    1   /*
    2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
    3    * 
    4    * This software is open source. 
    5    * See the bottom of this file for the licence.
    6    * 
    7    * $Id: SAXDOMDemo.java,v 1.4 2005/01/29 14:52:58 maartenc Exp $
    8    */
    9   
   10   package org.dom4j.samples.dom;
   11   
   12   import org.dom4j.samples.AbstractDemo;
   13   
   14   import org.dom4j.Document;
   15   import org.dom4j.io.DOMReader;
   16   import org.dom4j.io.DOMWriter;
   17   import org.dom4j.io.SAXContentHandler;
   18   import org.dom4j.io.SAXReader;
   19   import org.dom4j.io.SAXWriter;
   20   
   21   /**
   22    * This sample program parses an XML document as a DOM4J tree using SAX, it then
   23    * creates a W3C DOM tree which is then used as input for creating a new DOM4J
   24    * tree which is then output to SAX which is then parsed into another DOM4J tree
   25    * which is then output as XML.
   26    * 
   27    * This is clearly not terribly useful but demonstrates how to convert from SAX
   28    * <->DOM4J and DOM4J <->DOM and DOM4J <->text
   29    * 
   30    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
   31    * @version $Revision: 1.4 $
   32    */
   33   public class SAXDOMDemo extends AbstractDemo {
   34   
   35       public static void main(String[] args) {
   36           run(new SAXDOMDemo(), args);
   37       }
   38   
   39       public SAXDOMDemo() {
   40       }
   41   
   42       protected Document parse(String url) throws Exception {
   43           SAXReader saxReader = new SAXReader();
   44           Document document = saxReader.read(url);
   45   
   46           println("Parsed to DOM4J tree using SAX: " + document);
   47   
   48           // now lets make a DOM object
   49           DOMWriter domWriter = new DOMWriter();
   50           org.w3c.dom.Document domDocument = domWriter.write(document);
   51   
   52           println("Converted to DOM tree: " + domDocument);
   53   
   54           // now lets read it back as a DOM4J object
   55           DOMReader domReader = new DOMReader();
   56           document = domReader.read(domDocument);
   57   
   58           println("Converted to DOM4J tree using DOM: " + document);
   59   
   60           // now lets write it back as SAX events to
   61           // a SAX ContentHandler which should build up a new document
   62           SAXContentHandler contentHandler = new SAXContentHandler();
   63           SAXWriter saxWriter = new SAXWriter(contentHandler, null,
   64                   contentHandler);
   65   
   66           saxWriter.write(document);
   67           document = contentHandler.getDocument();
   68   
   69           println("Converted DOM4J to SAX events then back to DOM4J: " + document);
   70   
   71           return document;
   72       }
   73   }
   74   
   75   /*
   76    * Redistribution and use of this software and associated documentation
   77    * ("Software"), with or without modification, are permitted provided that the
   78    * following conditions are met:
   79    * 
   80    * 1. Redistributions of source code must retain copyright statements and
   81    * notices. Redistributions must also contain a copy of this document.
   82    * 
   83    * 2. Redistributions in binary form must reproduce the above copyright notice,
   84    * this list of conditions and the following disclaimer in the documentation
   85    * and/or other materials provided with the distribution.
   86    * 
   87    * 3. The name "DOM4J" must not be used to endorse or promote products derived
   88    * from this Software without prior written permission of MetaStuff, Ltd. For
   89    * written permission, please contact dom4j-info@metastuff.com.
   90    * 
   91    * 4. Products derived from this Software may not be called "DOM4J" nor may
   92    * "DOM4J" appear in their names without prior written permission of MetaStuff,
   93    * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
   94    * 
   95    * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
   96    * 
   97    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
   98    * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   99    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  100    * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
  101    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  102    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  103    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  104    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  105    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  106    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  107    * POSSIBILITY OF SUCH DAMAGE.
  108    * 
  109    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
  110    * 
  111    * $Id: SAXDOMDemo.java,v 1.4 2005/01/29 14:52:58 maartenc Exp $
  112    */

Save This Page
Home » dom4j-1.6.1 » org.dom4j.samples.dom » [javadoc | source]