Solved

Converting Dom Document to String

Posted on 2004-09-25
8
325 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
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 12150015
in my scenario, it should work for both xerces and xalan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
wordappend challenge 8 143
Java Loop 6 48
Apps blocked by Java 9 55
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now