?
Solved

Converting Dom Document to String

Posted on 2004-09-25
8
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

Assisted Solution

by:CEHJ
CEHJ earned 100 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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:
Suggested Courses

765 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