File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Document to File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Document to File" Watch "Document to File" New topic
Author

Document to File

Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
Hi,
how could we convert an object of type Document (org.w3c.dom.Document) to an object of type File.
Thanks
Regards
Chandhrasekar Saravanan
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I'm not sure what you wnat to do here.
A org.w3c.dom.Document is the root of an internal representation of some tree-structured data and provides access to lots of other objects which represent nodes, elements, attributes, text etc.
A java.io.File is a short note of where the bytes comprising a particular file are located on some media such as a Hard Disc.
They don't seem at all compatible. Can you explain in a bit more detail how you want one transformed into the other ?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
Hi Frank,
i need to find the content-length of the object of type document. if i could convert document to an object of type File,i would be able to find the content length. is there an any other way that we can find the content-length of Document object.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
But a Document doesn't have a "content length", it's just a bunch of objects. Are you assuming that an in-memory Document somehow contains the text you might find in an XML file on a disc? If so. I'm afraid it doesn't.
If you wish, you can write some code to traverse the nodes of the Document and write them out in some textual form. Once you have done that you can then get its length. But bear in mind that there are a potentially infinite number of ways of writing out an XML Document as text, so there is nothing definitive about the length you might get by doing this.
Can I ask why you need to do this? Maybe there's some way of solving the larger problem which doesn't need this step.
Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
Well, i am developing a simple SOAP Server. i have got a soap message in the document object.
and i need to write the HTTP headers. so i need to know the HTTP content-length of the
soap message to write to the HTTP header.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
At last, I understand. The answer is pretty straight forward, and the same as for any dynamically generated servlet output, really.
Write out your DOM as XML text to a StringBuffer rather than direct to the output stream, then use the length() of the StringBuffer as the content-length for your response, then send the contents of the StringBuffer as the body of the response.
Does that sound like a reasonable suggestion ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Document to File