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 Making JAXP spit out XML 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 "Making JAXP spit out XML" Watch "Making JAXP spit out XML" New topic
Author

Making JAXP spit out XML

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20528
    ∞

I have no problem making JAXP read in XML and tinker with it (I'm using DOM). But I want to have it spit XML into a string for me. How do I go about that?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I don't think the JAXP package has a convenient way to do that, but the Xerces parser is supposed to have a:
org.apache.xml.serialize.XMLSerializer
that will do it. I have not tried it yet.
That would seem to be an obvious thing to have in a XML toolkit, dunno why they left it out.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I guess that the reason they left it out is to avoid bloating the system for the few people who do need it. A basic DOM->XML processor is not hard to write, but it's easy to get bogged down in style issues (generate '<ugh/>' or '<ugh></ugh>'? use '<' or '<![CDATA[' ? and so on. I regard this is a fine example XML application, but not part of an XML parser.
My own XML parser uses subclassing for this. One class for the actual parser, with a child class which adds XML output. I've never used the XML output facility apart from internal tests, and for several applications I just don't include that class in the archive.
Most of my XML text is generated either by hand, using an XML tree editor or just a regular text editor, or using XSLT or some templating mechanism. Do you have an actual application in mind, or is this just a theoretical need?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20528
    ∞

I have a real need. I have an object that I want to make persistant. This object contains objects which contain objects. Our persistance layer is Oracle. I'm thinking that I'll store the sub objects as an XML string rather than making a bunch of tables.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Hey, I ask this question last week. I think I found the answer and an example but, have to look at it closer myself. It's at http://java.sun.com/xml/docs/tutorial/dom/index.html if you wish to use DOM or http://java.sun.com/xml/docs/tutorial/sax/2a_echo.html for the SAX I'm hoping this solves both our needs.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20528
    ∞

I dunno, that looks a lot more complicated than just writing a program that will write out basic XML from a DOM tree.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I've just seen this on the Resin mailing list, and haven't tried it yet...
> From: Brad Clow <brad@workingmouse.com>
>
> we use:
> document.getFirstChild().toString()
Anyone want to try it with JAXP or another DOM implementation?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
This is using the Xerces API
Where args[0] is a valid XML file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making JAXP spit out XML