Solved

Converting Dom Document to String

Posted on 2004-09-25
8
328 Views
Last Modified: 2013-11-23
I know How to Convert XML string to Document Object.

I want the result in other way. i.e. Document to string


0
Comment
Question by:sudhakar_koundinya
  • 6
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 12149998
Try

public String domToString(Document doc) {
      try {
            StringWriter sw = new StringWriter();
            OutputFormat output = new OutputFormat("text", "ISO-8859-1", true);
            output.setPreserveSpace(true);
            XMLSerializer serial = new XMLSerializer(sw, output);
            serial.serialize(doc);
            return sw.toString();
      }
      catch (IOException e) {
            e.printStackTrace();
            return null;
      }
}
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150006
Let me test that
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150010
OutputFormat

is this class universal or works for xerces only??
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150015
in my scenario, it should work for both xerces and xalan
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150036
Ok
I get the answer

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

//method to convert Document to String
public String getStringFromDocument(Document doc)
{
    try
    {
       DOMSource domSource = new DOMSource(doc);
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }
}
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150074
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 12150091
You beat me to it - was going to suggest the identity transform ;-)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12279767
Just to close this thread, I am awarding point to you

Regards
Sudhakar
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question