• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Create a XML file in Java

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
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
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
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic