Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create a XML file in Java

 
Miguel Roque
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I need to export some data from a DB to a XML file but I've never worked with XML in Java so I need your help. First of all, what is the best thecnology to use? I've seen JAXB, SAX, DOM and some others, but I don't know where to start here and what will be the best one to use.

In order to create the XML, I must follow the rules from a xsd file and the created XML will have several sections created, each of the sections being retrieved from a different table/tables.

Thanks,
Mike
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SAX is not suitable to create XML document as it doesn't allow manipulation of data in memory.

JAXB and DOM are useful to create XML documents. Building XML document using DOM API requires understanding the document tree structure and adding child nodes one after one which may be tedious.

JAXB API can be used to convert XML document into Java objects and vice versa. You can generate Java objects from xsd using binding compiler xjc provided by JAXB and load the Java objects with the data from database. Convert the objects into xml using JAXB API.


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also treat this as a problem of writing a formatted text file where the format required is valid XML conforming to a schema.

No XML library need be involved.

There are also cool tools such as ServingXML which can handle a wide variety of input formats and create XML.

Bill
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically, the "best" option for creating XML-based data files is to stay away from any "public" API such as DOM, SAX, JAXB, and create a custom API which is specific to the target markup language. Note that this applies for creating complex XML-based structures, not simple structures. Either DOM or JAXB can easily be used for the simple stuff

This option gives you the most degree of control and helps you avoid tightly coupling your code to a specific XML schema, i.e. do not attempt to use schema for "creating" XML-based files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic