• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making JAXP spit out XML

 
paul wheaton
Trailboss
Pie
Posts: 21388
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
paul wheaton
Trailboss
Pie
Posts: 21388
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul wheaton
Trailboss
Pie
Posts: 21388
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is using the Xerces API
Where args[0] is a valid XML file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic