• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAXP/W3C DOM Question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chris McGuirk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Debbie, try the alternate version of XmlDocument.write() that takes the encoding:

Susan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic