I usually just use a null Transformer (xalan), and transform it to a 
StringWriter.

give d the dom node

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
StringWriter sw = new StringWriter();
t.transform( new DOMSource(d), new StreamResult(sw));
return sw.toString();



-----Original Message-----
From: "Dunk, Michael (Mike)" <[EMAIL PROTECTED]>
Sent: Tue, June 12, 2007 11:58 am
To: j-users@xerces.apache.org
Subject: Sample DOMGenerate.java uses deprecated classes: Can anyone offer 
advice how to write out a DOM as a string?

Hi,

 

After installing Xerces 2.9.0, I was trying to base some code on the
sample class "DOMGenerate.java" in C:\Xerces\xerces-2_9_0\samples\dom.
See below for a full listing.

 

However, both OutputFormat and XMLSerializer are deprecated. Can anyone
offer advice how to write out a DOM as a string?

 

Best regards,

 

Mike

 

/*

 * Licensed to the Apache Software Foundation (ASF) under one or more

 * contributor license agreements.  See the NOTICE file distributed with

 * this work for additional information regarding copyright ownership.

 * The ASF licenses this file to You under the Apache License, Version
2.0

 * (the "License"); you may not use this file except in compliance with

 * the License.  You may obtain a copy of the License at

 * 

 *      http://www.apache.org/licenses/LICENSE-2.0

 * 

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

 

package dom;

 

import java.io.StringWriter;

 

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

 

import org.apache.xml.serialize.OutputFormat;

import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

 

/**

 * Simple Sample that:

 * - Generate a DOM from Scratch.

 * - Output DOM to a String using Serializer

 * @author Jeffrey Rodriguez

 * @version $Id: DOMGenerate.java 447683 2006-09-19 02:36:31Z mrglavas $

 */

public class DOMGenerate {

    public static void main( String[] argv ) {

        try {

            DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();

            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.newDocument();

            

            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();

        }

    }

}


This email and any files transmitted with it are confidential, proprietary
and intended solely for the individual or entity to whom they are addressed.
If you have received this email in error please delete it immediately.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to