Solved

Converting Dom Document to String

Posted on 2004-09-25
8
329 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 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
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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
login form jsp example 2 50
How to determine if a string is a valid SHA value 7 43
dao vs facade design patterns 2 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

749 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