Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6052615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:56:52+00:00 2026-05-23T07:56:52+00:00

How can I export data from database and store it in an XML file

  • 0

How can I export data from database and store it in an XML file using Hibernate with annotation.

Please tell me any way or tutorial that can help me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:56:52+00:00Added an answer on May 23, 2026 at 7:56 am

    Xml Import-Export In Java

    Prepare Database:

    SerializeExample.java: setObjects
    private static void setObjects()  {
        new File(DB4O_FILE_NAME).delete();
        ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME);
        try  {
          Car car = new Car("BMW", new Pilot("Rubens Barrichello"));
          container.store(car);
          car = new Car("Ferrari", new Pilot("Michael Schumacher"));
          container.store(car);
        } finally  {
          container.close();
        }
      }
    

    We will save the database to XML file “formula1.xml”:

    SerializeExample.java: exportToXml
    private static void exportToXml()  {
        XStream xstream = new XStream(new DomDriver());
        try  {
          FileWriter xmlFile = new FileWriter(XMLXML_FILE_NAME);
          ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME);
          try  {
            ObjectSet result = container.query(Car.class);
            Car[] cars = new Car[result.size()];
            for (int i = 0; i < result.size(); i++)  {
              Car car = (Car) result.next();
              cars[i] = car;
            }
            String xml = xstream.toXML(cars);
            xmlFile.write("<?xml version=\"1.0\"?>\n" + xml);
            xmlFile.close();
          } finally  {
            container.close();
          }
        } catch (Exception ex)  {
          System.out.println(ex.getMessage());
        }
      }
    

    Now we can clean the database and try to recreate it from the XML file:

    SerializeExample.java: importFromXml
    private static void importFromXml()  {
        new File(DB4O_FILE_NAME).delete();
        XStream xstream = new XStream(new DomDriver());
        try  {
          FileReader xmlReader = new FileReader(XMLXML_FILE_NAME);
          Car[] cars = (Car[]) xstream.fromXML(xmlReader);
          ObjectContainer container;
          for (int i = 0; i < cars.length; i++)  {
            container = Db4o.openFile(DB4O_FILE_NAME);
            try  {
              Car car = (Car) cars[i];
              container.store(car);
            } finally  {
              container.close();
            }
          }
          container = Db4o.openFile(DB4O_FILE_NAME);
          try  {
            ObjectSet result = container.query(Pilot.class);
            listResult(result);
            result = container.query(Car.class);
            listResult(result);
          } finally  {
            container.close();
          }
          xmlReader.close();
        } catch (Exception ex)  {
          System.out.println(ex.getMessage());
        }
      }
    

    Sample Code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any idea how to go about formatting the data so I can export from
We are using an extractor application that will export data from the database to
How can i export table data with column name in text file in sql
How can I export a GridView's data to a Microsoft Excel 2007 file? Also,
Is there any way by which we can export the result of a select
im currently want to inport my data from flat file to the database. the
I need to export data from a table in database A, then import it
I'm trying to export some data from MATLAB to a database. I use a
I'm trying to export data from a multivalue database (Unidata) into MySQL. Lets say
Ok, so I have the following script that will export data from my database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.