Home > Back-end >  multiple XML documents into a java object OR XML files to one java object
multiple XML documents into a java object OR XML files to one java object

Time:09-23

i have a number of different xml documents that don't basically have the same structures i need to parse them or marshal them into a java object. These xml documents come from different api's.

the java object is as follows the xml document is below the java object

@XmlRootElement(name = "ABF")
public class QuoteOffer   { 

    @XmlElement(name = "QUOTEID")
    private String quoteId;

    @XmlElement(name = "CHARGE")
    private Float charge; 

    @XmlElement(name = "SHIPDATE")
    private Date shipDate;

    @XmlElement(name = "EFFECTIVEDATE")
    private Date effectiveDate;

    @XmlElement(name = "EXPIRATIONDATE")
    private Date expirationDate; 

    @XmlElement(name = "ORIGTERMINFO")
    private List<OrigTermInfo> origTermInfo;

    @XmlElement(name = "DESTTERMINFO")
    private List<DestTermInfo> destTermInfo;

the xml document is as follows

<?xml version="1.0"?>
<ABF>
<QUOTEID>LS1N9F2601</QUOTEID>
<CHARGE>166.08</CHARGE>
<DISCOUNTPERCENTAGE></DISCOUNTPERCENTAGE>
<ADVERTISEDTRANSIT>1 Day</ADVERTISEDTRANSIT>
<ADVERTISEDDUEDATE>2021-05-06</ADVERTISEDDUEDATE>
<SHIPDATE>2021-05-05</SHIPDATE>
<EFFECTIVEDATE>2021-05-05</EFFECTIVEDATE>
<EXPIRATIONDATE>2021-05-11</EXPIRATIONDATE>
<CODFEE></CODFEE>
<TPDELIVERYCHARGE></TPDELIVERYCHARGE>
<TPCHARGEPERBOX></TPCHARGEPERBOX>
<ORIGTERMINFO>
<ORIGTERMADDRESS>4242 IRVING BLVD</ORIGTERMADDRESS>
<ORIGTERMCITY>DALLAS</ORIGTERMCITY>
<ORIGTERMSTATE>TX</ORIGTERMSTATE>
<ORIGTERMZIP>75247</ORIGTERMZIP>
<ORIGTERMPHONE>2146880448</ORIGTERMPHONE>
<TYPE>DIRECT</TYPE>
</ORIGTERMINFO>
<DESTTERMINFO>
<DESTTERMADDRESS>4410 S. JACKSON</DESTTERMADDRESS>
<DESTTERMCITY>TULSA</DESTTERMCITY>
<DESTTERMSTATE>OK</DESTTERMSTATE>
<DESTTERMZIP>74107</DESTTERMZIP>
<DESTTERMPHONE>9184460122</DESTTERMPHONE>
<TYPE>DIRECT</TYPE>
 
 
 

the code for xml and java object are truncated for easier readability. the java object is going to be fixed. if some fields are null so be it. the problem i am facing is that the xml documents will have different @XmlRootElements

1- (it doesnt make sense to have 20 different java objects to satisfy the 20 different xml documents and since i can only have one @XmlRootElement(name = "ABF") per java object this is causing a problem )

2- the @XmlElement can only be the same as the respective XML document so having different @XmlElement on one java field also doesnt make sense.

my question is does anyone know what technology to use to marshal multiple xml document values to a list of java objects.

i came across xml parsing and child node in a java loop. obviously i tried marshalling into java object using jaxb but this is not working. can someone direct me in the right direction so i can read more into it.

thanks

CodePudding user response:

I would suggest doing an XSLT transformation on the incoming documents to convert them to a common form prior to JAXB processing.

XSLT is much more flexible than JAXB in coping with variable XML document structures.

CodePudding user response:

DSM library is exactly solves this problem. You should have a look.

  • Related