0

XML which I want to unmarshall to java object:

           <API Name="A">
                     <Input>
                        <Shipment
                            EnterpriseCode="ABC" 
                            SellerOrganizationCode="6528"
                            ShipNode="" 
                             ShipmentKey="202105171041273117421546">
                            <ShipmentLines>
                                <ShipmentLine BackroomPickedQuantity="2.0" 
                                  ShipmentLineKey="41273117421545">
                                    <Extn 
                                ExtnStagingLocation="location1"/>
                                </ShipmentLine>
                            </ShipmentLines>
                            <Extn ExtnInPickupLocker="N" 
                                ExtnPickerId="1531685"
                                ExtnPickingHasStartedFlag="Y"
                                ExtnStagedByUserName="Windows and Walls" 
                                ExtnStagingLocation=""/>
                        </Shipment>
                    </Input>
                </API>
       

Below is the EXTN and EXTN1 java classes which I trying to map with XML have used JAXB and JACKSON to identify and map the variables with EXTN tag to java class Extn and Extn1.

                @XmlAccessorType(XmlAccessType.FIELD)
                    @Data
                    @ToString
                    @NoArgsConstructor
                    @AllArgsConstructor
                    public static class Extn {
                
                        @JsonProperty(value = "ExtnStagingLocation")
                        @JacksonXmlProperty(isAttribute = true)
                        private String extnStagingLocation = "location1";
                
                        @JsonProperty(value = "ExtnInPickupLocker")
                        @XmlAttribute(name = "ExtnInPickupLocker", required = true)
                        private String extnInPickupLocker = "N";
                
                        @JsonProperty(value = "ExtnPickerId")
                        @JacksonXmlProperty(isAttribute = true)
                        private String extnPickerId = "1531685";
                
                        @JsonProperty(value = "ExtnPickingHasStartedFlag")
                        @JacksonXmlProperty(isAttribute = true)
                        private String extnPickingHasStartedFlag = "Y";
                
                        @JsonProperty(value = "ExtnStagedByUserName")
                        @JacksonXmlProperty(isAttribute = true)
                        private String ExtnStagedByUserName = "Windows and Walls";
                    }
                    
                    @XmlAccessorType(XmlAccessType.FIELD)
                    @Data
                    @ToString
                    @NoArgsConstructor
                    @AllArgsConstructor
                    public static class Extn1 {
                
                        @JsonProperty(value = "ExtnStagingLocation")
                        @JacksonXmlProperty(isAttribute = true)
                        private String extnStagingLocation = "location1";
                    }
            

            

How do I Create a single class EXTN and then pass the desired attributes at different level while creating java object, rather creating different java class Extn and Extn1 and map the attributes inside the Extn tag

            Note: Have created EXTN and EXTN1 to pass different attribute and it's value at different place
andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • can you use a custom ExtnDeserializer that extends JsonDeserializer and annotate Extn @JsonDeserialize(using = ExtnDeserializer.class)? in ExtnDeserializer.deserialize you could look up for the 5 fields and istantiate a new Extn with them – tremendous7 May 17 '21 at 20:59
  • Have you looked into `Moxy`? It's a library based on `JAXB` but provides various other features. In `Moxy` you have an annotation called `@XmlPath` which can be used to map the element directly when you have `wrapper` tag. With this you do not need to create the `class` with single class you can manage everything. – BATMAN_2008 May 21 '21 at 10:51
  • 1
    @BATMAN_2008 , I used XmlPath and XmlAttribute annotations. Removed all the name and value pair from XmlAttribute and maintained one Extn class and declared variables in the required classes as required in xml file. Worked for me – QA_automationLearner Jun 06 '21 at 14:59
  • Thanks a lot for taking your time and responding. I am stuck in an issue with `@XmlPAth(".")` and `XMLAdapter`. If possible can you please have a look at this question and provide your answers: https://stackoverflow.com/q/67860792/7584240 – BATMAN_2008 Jun 06 '21 at 17:35

0 Answers0