Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes Document to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Document to String" Watch "Document to String" New topic
Author

Document to String

L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi,
I am using Xerces Java Parser 1.4.4. I need to get the String value of the Document object. toString() returns null !!!
Well I searched the API no method to get the String value of the XML Document object.
Prior I was using Electric XML parser which had the method to get the String value of the document.
Is it a DOM standard that Document should not provide method to get the String value?
Is there any way I can get the String value of the Document object.
Thanks for the time.


<b>L G Goundalkar</b><br /> <a href="mailto:lggoundalkar@yahoo.com" rel="nofollow">lggoundalkar@yahoo.com</a> <br />Sun Certified Programmer for Java 2 Platform.<br />Sun Certified Web Component Developer for J2EE.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
This should do it:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Is this all I need to use the above code? My compiler doesn't seem to know what a Source is.
import org.w3c.dom.*;
import org.xml.sax.*;

If not, what package(s) do I need, and is there a way to do it with only the above ones?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Thanks again. Its working fine.
Well Can you throw some light on this why its not given directly in the API ??
Cheers.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Can you throw some light on this why its not given directly in the API?
Sorry, don't know.
kiran manohar
Ranch Hand

Joined: Sep 28, 2003
Posts: 49
how about string to document?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by kiran manohar:
how about string to document?

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Originally posted by Lasse Koskela:
This should do it:


Any idea why I'm getting a NullPointerException? In the meantime, I've found another way to convert the XML doc into a string by serializing and then writing to a String, and here's that String:

Here's what it does to my Tomcat:

Note that the method in which I used your code is called convertXMLToString, and it takes in the XML doc.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you point out the exact line where the NullPointerException occurs?
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Thanks for your input...I'm getting a NullPointerException on the last line below:
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Strange. That would mean that the newTransformer() method would return a null. Well, it shouldn't, according to the javadoc:
[newTransformer() ... returns ...] A Transformer object that may be used to perform a transformation in a single thread, never null.

[ October 16, 2003: Message edited by: Lasse Koskela ]
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
In the meantime, I've found another way to convert the XML doc into a string by serializing and then writing to a String, and here's that String:


Hello Stephen!

How did you manage to serialize yoyr XML into a string?

I have tried:
java.io.ByteArrayOutputStream byteArrayOutputStream = new java.io.ByteArrayOutputStream();
java.io.ObjectOutputStream objectOutputStream = new java.io.ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(document);
objectOutputStream.flush();
System.out.println(byteArrayOutputStream.toString("ISO-8859-1"));

Best regards
Fredrik


Best regards<br />Fredrik
 
Consider Paul's rocket mass heater.
 
subject: Document to String
 
Similar Threads
Acessing value of attribute
Parser Pooling with JDOM
Load xml as String into DOMParser
Printing XML document in Java
XML Parser