xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeffrey Rodriguez" <jeffreyr...@hotmail.com>
Subject Re: building DOM tree from scratch
Date Wed, 31 May 2000 20:23:56 GMT
Mr. Pajkowski, Did you try the Serializers:

Here is some sample code from our DOMGenerate sample



import  org.w3c.dom.*;
import  org.apache.xerces.dom.DocumentImpl;
import  org.apache.xerces.dom.DOMImplementationImpl;
import  org.w3c.dom.Document;
import  org.apache.xml.serialize.OutputFormat;
import  org.apache.xml.serialize.Serializer;
import  org.apache.xml.serialize.SerializerFactory;
import  org.apache.xml.serialize.XMLSerializer;
import  java.io.*;


/**
* Simple Sample that:
* - Generate a DOM from Scratch.
* - Output DOM to a String using Serializer
* @author Jeffrey Rodriguez
* @version $id$
*/
public class DOMGenerate {
    public static void main( String[] argv ) {
        try {
            Document doc= new DocumentImpl();
            Element root = doc.createElement("person");     // Create Root 
Element
            Element item = doc.createElement("name");       // Create 
element
            item.appendChild( doc.createTextNode("Jeff") );
            root.appendChild( item );                       // atach element 
to Root element
            item = doc.createElement("age");                // Create 
another Element
            item.appendChild( doc.createTextNode("28" ) );
            root.appendChild( item );                       // Attach 
Element to previous element down tree
            item = doc.createElement("height");
            item.appendChild( doc.createTextNode("1.80" ) );
            root.appendChild( item );                       // Attach 
another Element - grandaugther
            doc.appendChild( root );                        // Add Root to 
Document



            OutputFormat    format  = new OutputFormat( doc );   //Serialize 
DOM
            StringWriter  stringOut = new StringWriter();        //Writer 
will be a String
            XMLSerializer    serial = new XMLSerializer( stringOut, format 
);
            serial.asDOMSerializer();                            // As a DOM 
Serializer

            serial.serialize( doc.getDocumentElement() );

            System.out.println( "STRXML = " + stringOut.toString() ); //Spit 
out DOM as a String
        } catch ( Exception ex ) {
            ex.printStackTrace();
        }
    }
}




>From: Keith Pajkowski <Keith.Pajkowski@T1Xpert.com>
>Reply-To: general@xml.apache.org
>To: general@xml.apache.org
>Subject: building DOM tree from scratch
>Date: Wed, 31 May 2000 14:26:29 -0400
>
>We want to convert a data structure to an XML equivalent on the fly, i.e.,
>build a DOM tree without actually reading in an XML document. The only
>example of this I can find is written in XML4J and made use of the (now
>deprecated) class TXDocument. In this example the completed document was
>output using the instruction:
>
>      ((TXDocument)doc).printWithFormat(new PrintWriter(System.out));
>
>I looked at the DOMWriter.java example in the latest XerxesJ release. When 
>I
>try to output the XML document using OutputStreamWriter, XercesJ prints the
>message:
>
>      [#document:  null]
>
>Sun's jaxp will not even compile the class, citing "Can't convert
>org.w3c.dom.Document to char[]".
>
>Any references you can send would be much appreciated.
>
>
>Keith Pajkowski
>T1Xpert.com
>212 771 1059
>
>

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


Mime
View raw message