• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Java & XML

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never done this in Java...and I'm sure it's easy...but I'm finding a *lot* of crud while googling the topic.

Here's what I'm after; I simply need to generate xml from a database. Which API would I use to create an XML document while using JDBC to loop through some read-only records?

I've found some info on SAX and DOM and apparently there's an API that ties them together? I'm confused.

Thanks!
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinnie Jenks:

I've found some info on SAX and DOM and apparently there's an API that ties them together? I'm confused.

Thanks!



SAX and DOM are two different APIs used to parse XML documents. SAX is event based -- using callbacks to signify start of tags, end of tags, data, etc. You have to parse the data from the events sent. DOM does the parsing and returns an tree object structure that represents the document. You must then traverse this structure to use the data.

Furthermore, I think DOM uses SAX under the covers -- but I may be wrong on this one.

Henry
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, forgot to answer your question.

On the few occasions that I needed to generate XML documents -- from hashtables -- I just wrote the data directly to files. (XML documents are just text files)

Henry
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can build an XML DOM in memory and then write it out to an XML string. It's a bit tedious but certainly something that people do all the time. Scroll on down to the XML forum on the ranch and ask for pointers to some introductory examples.

You have your choice of the W3C DOM defined in the Java APIs or some simpler alternatives. JDom was invented just to make things easier for Java folks.

You can also just build XML as a giant StringBuffer. At first this seems easier to follow because you don't have to learn your way around the DOM, but after a certain level of complexity keeping track of the structure in a string gets difficult, too.

Any of that sound on target to your question?
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go:

http://www.cafeconleche.org/books/xmljava/chapters/index.html

The part of Chapter 8 entitled "Parsing non-XML Documents" is just what you asked for. But don't dismiss the rest of the book as "crud". You can't learn XML processing in Java in a day.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which database are you using? Some (Oracle and SQL Server are the ones that spring to mind) will produce results as XML, which you could just parse rather than building a DOM.
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, very helpful!

Paul, this example on the url you provided is exactly what I was looking for:

http://www.cafeconleche.org/books/xmljava/chapters/ch03s03.html#FibonacciFile.java

It's not pretty but it's all I need...I have some simple schemas to conform to and I just need some basic XML from data. And no doubt, I'll not learn Java XML processing in a day...but Java certainly has the horsepower to do anything you need, eh?

Thanks much guys!
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic