File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes JAXP/W3C DOM Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXP/W3C DOM Question" Watch "JAXP/W3C DOM Question" New topic
Author

JAXP/W3C DOM Question

Chris McGuirk
Greenhorn

Joined: Jan 16, 2001
Posts: 7
Does anyone know an easy way to retreive a string representation of a XML document? I have used an older Java XML parser from DataChannel, and their Document object had a getXML function which returned a string. We are looking to replace this with the newer JAXP parser. Thanks in advance.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
You need the whole XML document as a string, including the tags?? If that's what you need, then using a parser would be an overkill. Just treat the XML document as an inputstream and read the contents using standard I/O stream methods.
On the otherhand if you need to extract all the data in the XML and concatenate them as a string, you will need a parser and I suggest you use a SAX parser. DOM is a choice only if you need random access to nodes and data manipulation features.
I'm not sure if I understood your question correctly. Please clarify.

------------------

Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Chris McGuirk
Greenhorn

Joined: Jan 16, 2001
Posts: 7
I have loaded a document into a DOM Document already via an InputStream. I would like to know if it is possible to get a string representation of the XML out of the existing Document object, for example, to print out to the console for inspection.
Chris McGuirk
Greenhorn

Joined: Jan 16, 2001
Posts: 7
...i.e. The DOMDocument object in the MSXML parser has a XML property, which is an XML string representation of the entire document.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
The only thing I can think of is a tree-traversal. Starting from the document root you traverse the nodes, printing out the child nodes of the current node recursively. Java and XML book by O'Reilly has an example code to do this.
On the otherhand since you already have the handle to InputStream, you can use Java I/O stream to read the file "externally" and dump the contents. This works fine if you just want to display the XML document for debugging purposes.

------------------

Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
debbie collins
Greenhorn

Joined: Oct 11, 2000
Posts: 9
You can use jaxp to write an XML text file from a Document.
import com.sun.xml.tree.XMLDocument;
import org.w3c.dom.Document;

FileWriter oWriter = new FileWriter( FileName );
((XmlDocument)document).write( oWriter );

I've been trying this out today and it works. I'm having a problem with the header that is generated, however. It is:
<?xml version="1.0" encoding="Cp1252"?>
IE5 doesn't recognize this encoding. Anyone know why or what I can do to make it recognize it?
Susan Hoover
Ranch Hand

Joined: Jan 04, 2001
Posts: 64
Debbie, try the alternate version of XmlDocument.write() that takes the encoding:

Susan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXP/W3C DOM Question