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 9256121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:52:02+00:00 2026-06-18T11:52:02+00:00

What I want to do I want to export several different Java objects to

  • 0

What I want to do

I want to export several different Java objects to XML. In order to minimize code duplication, I took the following approach:

I made an abstract class implementing the creation of a standard empty DOM document and the transformer factory part.

Then I added an abstract method for filling the DOM document with the specific object data.
I then made a CustomerExporter class which extended the abstract class and implemented the abstract method for that specific object.

I got that to work.

My question

How do write the abstract method so that it can cope with the different Java objects?

The problem

The objects are of different types so the attributes are not always the same. So an Interface wouldn’t work (or would it?).

The code

Customer class

package org.flexdataexoporter.dao;

public class Customer{

    private int customerID;
    private String firstname;
    private String lastname;
    private int telnumber;

    public Customer(int customerID, String firstname, String lastname, int telnumber){

        this.customerID = customerID;
        this.firstname = firstname;
        this.lastname = lastname;
        this.telnumber = telnumber;
    }

    public int getCustomerID(){
        return customerID;
    }

    public String getFirstName(){
        return firstname;
    }

    public String getLastName(){
        return lastname;
    }

    public int getTelNumber(){
        return telnumber;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setTelnumber(int telnumber) {
        this.telnumber = telnumber;
    }
}

Customers class

package org.flexdataexoporter.dao;

import java.util.ArrayList;

public class Customers extends ArrayList<Customer>  {

    private static final long serialVersionUID = 1L;

    public Customers(){ };

}

Abstract exporter class

package org.flexdataexporter.export;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.flexdataexoporter.dao.Customers;
import org.w3c.dom.Document;

public abstract class XMLExporter {

    public void generateXML(Customers customers) throws ParserConfigurationException, TransformerException{
        Document doc = createDomDoc();
        fillXML(doc, customers);
    }

    protected Document createDomDoc() throws ParserConfigurationException{
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        return doc;
    }

    public abstract void fillXML(Document doc, Customers customers) throws TransformerException;


    protected void writeDoc(Document doc) throws TransformerException{

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    }

}

Specific export class

package org.flexdataexporter.export;

import javax.xml.transform.TransformerException;

import org.flexdataexoporter.dao.Customer;
import org.flexdataexoporter.dao.Customers;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CustomerExporter extends XMLExporter {

    public CustomerExporter(){ }

    @Override
    public void fillXML(Document doc, Customers customers) throws TransformerException {
        Element rootElement = doc.createElement("customers");
        doc.appendChild(rootElement);

        for(Customer c  : customers){
            Element customer = doc.createElement("customer");
            rootElement.appendChild(customer);
            customer.setAttribute("id", Integer.toString(c.getCustomerID()));

            Element name = doc.createElement("firstname");
            name.appendChild(doc.createTextNode(c.getFirstName()));
            customer.appendChild(name);

            Element lastname = doc.createElement("lastname");
            lastname.appendChild(doc.createTextNode(c.getLastName()));
            customer.appendChild(lastname);

            Element phone = doc.createElement("phone");
            phone.appendChild(doc.createTextNode(Integer.toString(c.getTelNumber())));
            customer.appendChild(phone);
            super.writeDoc(doc);
        }
    }
}

I hope someone can help me with a solution.

  • 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-06-18T11:52:03+00:00Added an answer on June 18, 2026 at 11:52 am

    I would suggest looking for a library to do this for you. But assuming you need to write it your self, I think what you want is generics

    public abstract class XMLExporter<T> {
        public void generateXML(T object) throws ParserConfigurationException, TransformerException{
            Document doc = createDomDoc();
            fillXML(doc, object);
        }
    
        public abstract void fillXML(Document doc, T object) throws TransformerException;
    }
    

    And then your implementing class

    public class CustomerExporter extends XMLExporter<Customers> {
    
    }
    

    I haven’t used Java in a while so my syntax maybe wrong or incomplete but hopefully this gets going where you wanted to go.

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

Sidebar

Related Questions

I've got a load of DataTables that I want to export into several Excel
I want to export a custom XML feed from Magento, and this is the
I want to create several workspaces which point to different branches of a codebase.
I'm thinking about using pentahose to help me transform different xml files from several
I want to write several functions that are only different in the types of
I have a several rows in my table and I want to export the
I want to execute several commands by PHP, //DISPLAY setting $command1 = 'export DISPLAY=:1';
I want to export some classes, say, Dog and Cat . One way to
I want to export TVP (Type of DataTable) to .csv file, or .xls, but
I want to export my simple application in xcode 4.33 But I failed to

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.