• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java & XML  RSS feed

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!