Skip to main content
Published: January 05 2012, 5:39:00 PMUpdated: November 28 2020, 10:11:14 PM

Summary  

  You can use the JAXB Unmarshaller interface to inflate an XML document into a java object or to deserialize java objects from xml.   This sample illustrates the use of JAVA JAXB framework to unzip the file returned in downloadFile API 
 and unmarshal the Merchant Data xml document that based on Merchant Data Schema.

  To run this program, you will need to first generate java classes that represent the Marchant Data Schema.  Pleaes refer to the construct LMS data files with eBay Merchant Data Schema in JAVA kb for the codeGen ant script for the same.

 Also, you can check the attached Java source code for the implementation of the BulkDataExchangeResponse xml for ReviseFixedPriceItem call.

import ebay.apis.eblbasecomponents.BulkDataExchangeResponsesType;
import ebay.apis.eblbasecomponents.ExternalTransactionType;
import ebay.apis.eblbasecomponents.OrderDetailsType;
import ebay.apis.eblbasecomponents.SoldReportResponseType;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;


public class SoldReport{

    private static String jaxb_object_tree_package_name = "ebay.apis.eblbasecomponents";
    private static String destinationPathForUnzipped = "C:\\tmp\\unzipped\\";

    public static void main(String[] args) {

        String soldReportFilePath="c:\\5024432353_report.gz";
        BulkDataExchangeResponsesType bd = (BulkDataExchangeResponsesType)readResponse(soldReportFilePath);
         SoldReportResponseType soldreport = bd.getSoldReport();


            List<OrderDetailsType> list = soldreport.getOrderDetails();
            for (OrderDetailsType details : list) {
                System.out.println(details.getOrderID());
                System.out.println(details.getBuyerUserID());
                System.out.println(details.getOrderCreationTime());         

                ....
            }
    }

    public static BulkDataExchangeResponsesType readResponse(String downloadZipFileName) {
        BulkDataExchangeResponsesType bd = null;
        Object obj = null;
        try {
            String unzippedFile = getZipFiles(downloadZipFileName,destinationPathForUnzipped);
            JAXBContext jc = JAXBContext.newInstance(jaxb_object_tree_package_name);
            // create an Unmarshaller
            Unmarshaller u = jc.createUnmarshaller();
            JAXBElement<Object> poe = (JAXBElement) u.unmarshal(new FileInputStream(unzippedFile));
            obj = poe.getValue();
            bd = (BulkDataExchangeResponsesType) obj;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bd;
    }


  public static String getZipFiles(String filename , String destinationPath )
    {
         String unzippedEntryName=null;
        try
        {

            byte[] buf = new byte[1024];
            ZipInputStream zipinputstream = null;
            ZipEntry zipentry;
            zipinputstream = new ZipInputStream(
                new FileInputStream(filename));

            zipentry = zipinputstream.getNextEntry();

            while (zipentry != null)
            {
                //for each entry to be extracted
                unzippedEntryName = zipentry.getName();
                System.out.println("entryname "+unzippedEntryName);

                int n;
                FileOutputStream fileoutputstream;
                File newFile = new File(unzippedEntryName);
                String directory = newFile.getParent();
                if(directory == null)
                {
                    if(newFile.isDirectory())
                        break;
                }

                fileoutputstream = new FileOutputStream(
                   destinationPath+unzippedEntryName);

                while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
                    fileoutputstream.write(buf, 0, n);

                fileoutputstream.close();
                zipinputstream.closeEntry();
                zipentry = zipinputstream.getNextEntry();

            }//while

            zipinputstream.close();
        }
        catch (Exception e)
        {
            //
        }
        return destinationPath+ unzippedEntryName;
    }

}

How well did this answer your question?
Answers others found helpful