• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Document to String

 
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should do it:
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you throw some light on this why its not given directly in the API?

Sorry, don't know.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about string to document?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by kiran manohar:
how about string to document?


 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you point out the exact line where the NullPointerException occurs?
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input...I'm getting a NullPointerException on the last line below:
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!