Solved

Converting Dom Document to String

Posted on 2004-09-25
8
321 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

18 Experts available now in Live!

Get 1:1 Help Now