Home » Xerces-J-src.2.9.1 » dom » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package dom;
   19   
   20   import java.io.StringWriter;
   21   
   22   import javax.xml.parsers.DocumentBuilder;
   23   import javax.xml.parsers.DocumentBuilderFactory;
   24   
   25   import org.w3c.dom.Document;
   26   import org.w3c.dom.Element;
   27   import org.w3c.dom.bootstrap.DOMImplementationRegistry;
   28   import org.w3c.dom.ls.DOMImplementationLS;
   29   import org.w3c.dom.ls.LSOutput;
   30   import org.w3c.dom.ls.LSSerializer;
   31   
   32   /**
   33    * Simple Sample that:
   34    * - Generates a DOM from scratch.
   35    * - Writes the DOM to a String using an LSSerializer
   36    * @author Jeffrey Rodriguez
   37    * @version $Id: DOMGenerate.java 546623 2007-06-12 20:25:08Z mrglavas $
   38    */
   39   public class DOMGenerate {
   40       
   41       public static void main( String[] argv ) {
   42           try {
   43               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   44               dbf.setNamespaceAware(true);
   45               DocumentBuilder db = dbf.newDocumentBuilder();
   46               Document doc = db.newDocument();
   47               
   48               Element root = doc.createElementNS(null, "person"); // Create Root Element
   49               Element item = doc.createElementNS(null, "name");   // Create element
   50               item.appendChild( doc.createTextNode("Jeff") );
   51               root.appendChild( item );                           // Attach element to Root element
   52               item = doc.createElementNS(null, "age");            // Create another Element
   53               item.appendChild( doc.createTextNode("28" ) );       
   54               root.appendChild( item );                           // Attach Element to previous element down tree
   55               item = doc.createElementNS(null, "height");            
   56               item.appendChild( doc.createTextNode("1.80" ) );
   57               root.appendChild( item );                           // Attach another Element - grandaugther
   58               doc.appendChild( root );                            // Add Root to Document
   59   
   60               DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
   61               DOMImplementationLS domImplLS = (DOMImplementationLS)registry.getDOMImplementation("LS");
   62               
   63               LSSerializer ser = domImplLS.createLSSerializer();  // Create a serializer for the DOM
   64               LSOutput out = domImplLS.createLSOutput();
   65               StringWriter stringOut = new StringWriter();        // Writer will be a String
   66               out.setCharacterStream(stringOut);
   67               ser.write(doc, out);                                // Serialize the DOM
   68   
   69               System.out.println( "STRXML = " 
   70                       + stringOut.toString() );                   // Spit out the DOM as a String
   71           } catch ( Exception ex ) {
   72               ex.printStackTrace();
   73           }
   74       }
   75   }
   76   

Home » Xerces-J-src.2.9.1 » dom » [javadoc | source]