• 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

Making JAXP spit out XML

 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is using the Xerces API
Where args[0] is a valid XML file.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic