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

  • SEARCH
  • Home
  • 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 3595984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:55:28+00:00 2026-05-18T19:55:28+00:00

I have to create object model for following XMLs: XML sample 1: <InvoiceAdd> <TxnDate>2009-01-21</TxnDate>

  • 0

I have to create object model for following XMLs:

XML sample 1:

<InvoiceAdd>
  <TxnDate>2009-01-21</TxnDate>
  <RefNumber>1</RefNumber>
  <InvoiceLineAdd>
  </InvoiceLineAdd>
</InvoiceAdd>

XML Sample 2:

<SalesOrderAdd>
  <TxnDate>2009-01-21</TxnDate>
  <RefNumber>1</RefNumber>
  <SalesOrderLineAdd>
  </SalesOrderLineAdd>
</SalesOrderAdd>

The XML output will be based on a single string parameter or enum. String txnType = “Invoice”; (or “SalesOrder”);

I would use single class TransactionAdd:

@XmlRootElement
public class TransactionAdd {  
  public String txnDate;
  public String refNumber;

  private String txnType;
  ...

  public List<LineAdd> lines;
}

instead of using subclasses or anything else. The code which creates the TransactionAdd instance is the same for both types of transaction it only differs on the type.

This XML is used by a rather known product called QuickBooks and is consumed by QuickBooks web service – so I can’t change the XML, but I want to make it easy to be able to set element name based on property (txnType).

I would consider something like a method to determine target element name:

@XmlRootElement
public class TransactionAdd {  
  public String txnDate;
  public String refNumber;

  private String txnType;
  ...

  public List<LineAdd> lines;

  public String getElementName() {
     return txnType + "Add";
  }
}

Different transactions will be created using following code:

t = new TransactionAdd();
t.txnDate = "2010-12-15";
t.refNumber = "123";
t.txnType = "Invoice";

The goal is to serialize t object with the top-level element name based on txnType. E.g.:

<InvoiceAdd>
   <TxnDate>2009-01-21</TxnDate>
   <RefNumber>1</RefNumber>
</InvoiceAdd>

In case of t.txnType = “SalesOrder” the result should be

<SalesOrderAdd>
   <TxnDate>2009-01-21</TxnDate>
   <RefNumber>1</RefNumber>
</SalesOrderAdd>

At the moment I see only one workaround with subclasses InvoiceAdd and SalesOrderAdd and using @XmlElementRef annotation to have a name based on class name. But it will need to instantiate different classes based on transaction type and also will need to have two other different classes InvoiceLineAdd and SalesOrderLineAdd which looks rather ugly.

Please suggest me any solution to handle this. I would consider something simple.

  • 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-18T19:55:29+00:00Added an answer on May 18, 2026 at 7:55 pm

    To address the root element aspect you could do will need to leverage @XmlRegistry and @XmlElementDecl. This will give us multiple possible root elements for the TransactionAdd class:

    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.XmlElementDecl;
    import javax.xml.bind.annotation.XmlRegistry;
    import javax.xml.namespace.QName;
    
    @XmlRegistry
    public class ObjectFactory {
    
        @XmlElementDecl(name="InvoiceAdd")
        JAXBElement<TransactionAdd> createInvoiceAdd(TransactionAdd invoiceAdd) {
            return new JAXBElement<TransactionAdd>(new QName("InvoiceAdd"), TransactionAdd.class, invoiceAdd);
        }
    
        @XmlElementDecl(name="SalesOrderAdd")
        JAXBElement<TransactionAdd> createSalesOrderAdd(TransactionAdd salesOrderAdd) {
            return new JAXBElement<TransactionAdd>(new QName("SalesOrderAdd"), TransactionAdd.class, salesOrderAdd);
        }
    
    }
    

    Your TransactionAdd class will look something like the following. The interesting thing to note is that we will make the txnType property @XmlTransient.

    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    public class TransactionAdd {
    
        private String txnDate;
        private String refNumber;
        private String txnType;
        private List<LineAdd> lines;
    
        @XmlElement(name="TxnDate")
        public String getTxnDate() {
            return txnDate;
        }
    
        public void setTxnDate(String txnDate) {
            this.txnDate = txnDate;
        }
    
        @XmlElement(name="RefNumber")
        public String getRefNumber() {
            return refNumber;
        }
    
        public void setRefNumber(String refNumber) {
            this.refNumber = refNumber;
        }
    
        @XmlTransient
        public String getTxnType() {
            return txnType;
        }
    
        public void setTxnType(String txnType) {
            this.txnType = txnType;
        }
    
        public List<LineAdd> getLines() {
            return lines;
        }
    
        public void setLines(List<LineAdd> lines) {
            this.lines = lines;
        }
    
    }
    

    Then we need to supply a little logic outside the JAXB operation. For an unmarshal we will use the local part of the root element name to populate the txnType property. For a marshal we will use the value of the txnType property to create the appropriate JAXBElement.

    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(TransactionAdd.class, ObjectFactory.class);
    
            File xml = new File("src/forum107/input1.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement<TransactionAdd> je = (JAXBElement<TransactionAdd>) unmarshaller.unmarshal(xml);
            TransactionAdd ta = je.getValue();
            ta.setTxnType(je.getName().getLocalPart());
    
            JAXBElement<TransactionAdd> jeOut;
            if("InvoiceAdd".equals(ta.getTxnType())) {
                jeOut = new ObjectFactory().createInvoiceAdd(ta);
            } else {
                jeOut = new ObjectFactory().createSalesOrderAdd(ta);
            }
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(jeOut, System.out);
        }
    
    }
    

    To Do

    I will look into addressing the lines property next.

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

Sidebar

Related Questions

I have to create object model for following XMLs: XML sample 1: <InvoiceAdd> <TxnDate>2009-01-21</TxnDate>
I have inherited a class in vb.net and when I create the object, I
I have the need to take a string argument and create an object of
Suppose we have a class. We create an object from the class and when
I have a problem with my WPF program. I'm trying to create an object
I have a VB6 dll that is trying to create a COM object using
I have a field object and I create a list of fields: class Field
I have a need to create a library of Object Oriented PHP code that
I have a Rect object that I'd like to create and set its properties
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT

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.