XStream library unable to convert XMLGregorianCalendar

821 Views Asked by At

I am trying to convert the WSDL file to a java class and build java XML requests as java objects and trying print in the console using the Xstream library. The issue here is XML request as date time XML tag as XMLGregoriancalender as data type. when pass XML Java object to new XStream().toxml(java object); It provide the following error. Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: No converter available.

XML DATA:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:glob="http://sap.com/xi/SAPGlobal20/Global">
   <soapenv:Header/>
   <soapenv:Body>
      <glob:PLCLogCreateRequest_sync>
         <PLCLog>
            <DateTime>2022-06-15</DateTime>
            <Lecturas>
               <BAL>4</BAL>
               <INICIO>78671.20</INICIO>
               <FINAL>146082.10</FINAL>
            </Lecturas>
            <Lecturas>
               <BAL>5</BAL>
               <INICIO>52528.20</INICIO>
               <FINAL>106812.45</FINAL>
            </Lecturas>
            <Lecturas>
               <BAL>6</BAL>
               <INICIO>44286.53</INICIO>
               <FINAL>90948.79</FINAL>
            </Lecturas>
            <Lecturas>
               <BAL>7</BAL>
               <INICIO>38912.68</INICIO>
               <FINAL>79438.60</FINAL>
            </Lecturas>
         </PLCLog>
      </glob:PLCLogCreateRequest_sync>
   </soapenv:Body>
</soapenv:Envelope>


// soap request to create PLC log

                PLCLogCreateRequestMessageSync plcLogCreateRequestMessageSync = new PLCLogCreateRequestMessageSync();
                plcLogCreateRequestMessageSync.setBasicMessageHeader(null);

                PLCLogCreateRequest plcLogCreateRequest = new PLCLogCreateRequest();

                for (HashMap<String, String> map : requiredCompleteDataInArrayList) {

                    PLCLogCreateRequestBalanceReading plcLogCreateRequestBalanceReading = new PLCLogCreateRequestBalanceReading();
                    plcLogCreateRequestBalanceReading.setBalance(map.get("BAL_VALUES").replaceAll("\\s", ""));
                    plcLogCreateRequestBalanceReading.setInico(new BigDecimal(map.get("INITIAL_LINES")));
                    plcLogCreateRequestBalanceReading.setFinal(new BigDecimal(map.get("LAST_LINES")));
                    plcLogCreateRequest.getBalanceReading().add(plcLogCreateRequestBalanceReading);
                }

                DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
                
            String tempDate =   lastLine[0].replace("/", "-");
            System.out.println(tempDate.replace("\"", ""));
            
            
                
                Date date = format.parse(tempDate.replace("\"", ""));

                GregorianCalendar cal = new GregorianCalendar();
                cal.setTime(date);
                
                XMLGregorianCalendar xmlGregCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

                DateTime dateTime = new DateTime();
                dateTime.setValue(xmlGregCal);

                plcLogCreateRequest.setReadingDateTime(dateTime);
                plcLogCreateRequestMessageSync.setPLCLog(plcLogCreateRequest);
                System.out.println(plcLogCreateRequestMessageSync);
                
                
                System.out.println( new XStream().toXML(plcLogCreateRequestMessageSync));

ERROR:

message             : No converter available
type                : com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl
converter           : com.thoughtworks.xstream.converters.reflection.SerializableConverter
message[1]          : Unable to make private void com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.readObject(java.io.ObjectInputStream) throws java.lang.ClassNotFoundException,java.io.IOException accessible: module java.xml does not "opens com.sun.org.apache.xerces.internal.jaxp.datatype" to unnamed module @2e005c4b
converter[1]        : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[2]          : Unable to make field private transient java.math.BigInteger com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.orig_eon accessible: module java.xml does not "opens com.sun.org.apache.xerces.internal.jaxp.datatype" to unnamed module @2e005c4b
1

There are 1 best solutions below

0
Basil Bourque On

tl;dr

LocalDate.parse( "2022-06-15" )

Details

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

Your data has only a date. In contrast, a XMLGregorianCalendar object represents a date, day of time, and a time zone. So that class does not fit the data, in addition to being legacy.

For date only values, use LocalDate.

Your input 2022-06-15 is text in standard ISO 8601 format. The ISO 8601 formats are used by default in java.time for parsing/generating text. So no need to define a formatting pattern.

LocalDate ld = LocalDate.parse( "2022-06-15" ) ;