|
| 1 | +package com.howtodoinjava.jaxb; |
| 2 | + |
| 3 | +import jakarta.xml.bind.JAXBContext; |
| 4 | +import jakarta.xml.bind.JAXBException; |
| 5 | +import jakarta.xml.bind.Marshaller; |
| 6 | +import jakarta.xml.bind.Unmarshaller; |
| 7 | +import jakarta.xml.bind.annotation.XmlAccessType; |
| 8 | +import jakarta.xml.bind.annotation.XmlAccessorType; |
| 9 | +import jakarta.xml.bind.annotation.XmlRootElement; |
| 10 | +import jakarta.xml.bind.annotation.XmlTransient; |
| 11 | +import jakarta.xml.bind.annotation.adapters.XmlAdapter; |
| 12 | +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
| 13 | +import java.io.StringReader; |
| 14 | +import java.io.StringWriter; |
| 15 | +import lombok.AllArgsConstructor; |
| 16 | +import lombok.Data; |
| 17 | +import lombok.NoArgsConstructor; |
| 18 | + |
| 19 | +public class NoDefaultConstructor { |
| 20 | + |
| 21 | + public static void main(String[] args) throws JAXBException { |
| 22 | + Address address = new Address("Line 1", "Line 2", "New Delhi", "India", new PinCode("110075")); |
| 23 | + JAXBContext jaxbContext = JAXBContext.newInstance(Address.class); |
| 24 | + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); |
| 25 | + StringWriter stringWriter = new StringWriter(); |
| 26 | + jaxbMarshaller.marshal(address, stringWriter); |
| 27 | + String XML = stringWriter.toString(); |
| 28 | + System.out.println(XML); |
| 29 | + |
| 30 | + String xmlAddress = """ |
| 31 | + <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 32 | + <Address> |
| 33 | + <line1>Line 1</line1> |
| 34 | + <line2>Line 2</line2> |
| 35 | + <city>New Delhi</city> |
| 36 | + <country>India</country> |
| 37 | + <pinCode> |
| 38 | + <code>110075</code> |
| 39 | + </pinCode> |
| 40 | + </Address> |
| 41 | + """; |
| 42 | + |
| 43 | + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); |
| 44 | + Address newAddress = (Address) jaxbUnmarshaller.unmarshal(new StringReader(xmlAddress)); |
| 45 | + System.out.println(newAddress); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +@Data |
| 50 | +@NoArgsConstructor |
| 51 | +@AllArgsConstructor |
| 52 | +@XmlRootElement(name = "Address") |
| 53 | +@XmlAccessorType(XmlAccessType.FIELD) |
| 54 | +class Address { |
| 55 | + String line1; |
| 56 | + String line2; |
| 57 | + String city; |
| 58 | + String country; |
| 59 | + //@XmlTransient |
| 60 | + @XmlJavaTypeAdapter(PinCodeAdapter.class) |
| 61 | + PinCode pinCode; |
| 62 | +} |
| 63 | +@Data |
| 64 | +class PinCode { |
| 65 | + String code; |
| 66 | + |
| 67 | + public PinCode(String code){ |
| 68 | + this.code = code; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@Data |
| 73 | +class XmlPinCode { |
| 74 | + |
| 75 | + String code; |
| 76 | + public XmlPinCode(){ |
| 77 | + } |
| 78 | + |
| 79 | + public XmlPinCode(String code){ |
| 80 | + this.code = code; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class PinCodeAdapter extends XmlAdapter<XmlPinCode, PinCode> { |
| 85 | + |
| 86 | + @Override |
| 87 | + public PinCode unmarshal(XmlPinCode v) throws Exception { |
| 88 | + return new PinCode(v.getCode()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public XmlPinCode marshal(PinCode v) throws Exception { |
| 93 | + return new XmlPinCode(v.getCode()); |
| 94 | + } |
| 95 | +} |
0 commit comments