Skip to content

Commit d0eb423

Browse files
committed
jaxb final
1 parent 0f28223 commit d0eb423

6 files changed

Lines changed: 67 additions & 18 deletions

File tree

java-xml/src/main/java/com/mkyong/xml/jaxb/JaxbExample.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.mkyong.xml.jaxb;
22

33
import com.mkyong.xml.jaxb.model.Company;
4+
import com.mkyong.xml.jaxb.model.Fruit;
45
import com.mkyong.xml.jaxb.model.Staff;
56

67
// @Since 3.0.0, rebrand to jakarta.xml
78
import jakarta.xml.bind.JAXBContext;
89
import jakarta.xml.bind.JAXBException;
910
import jakarta.xml.bind.Marshaller;
11+
import jakarta.xml.bind.Unmarshaller;
1012

1113
// old APIs 2.3.*,
1214
//import javax.xml.bind.JAXBContext;
1315
//import javax.xml.bind.JAXBException;
1416
//import javax.xml.bind.Marshaller;
1517

18+
import java.io.File;
1619
import java.time.ZonedDateTime;
1720
import java.util.Arrays;
1821

@@ -23,11 +26,10 @@ public static void main(String[] args) {
2326
JAXBContext jaxbContext = null;
2427
try {
2528

26-
//jaxbContext = JAXBContext.newInstance(Staff.class);
2729
//jaxbContext = JAXBContext.newInstance(Company.class);
2830

29-
// EclipseLink MOXy jaxb.properties need same package with the Company.class or Staff.class
30-
// Alternative, define this via eclipse JAXBContextFactory manually.
31+
// EclipseLink MOXy needs jaxb.properties at the same package with Company.class or Staff.class
32+
// Alternative, I prefer define this via eclipse JAXBContextFactory manually.
3133
jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
3234
.createContext(new Class[] {Company.class}, null);
3335

@@ -36,19 +38,26 @@ public static void main(String[] args) {
3638
// output pretty printed
3739
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
3840

39-
// change encoding
40-
// jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
41+
// change XML encoding
42+
//jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
4143

4244
// remove <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
43-
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
45+
//jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
4446
//jaxbMarshaller.setProperty("com.sun.xml.bind.xmlDeclaration", false);
4547

4648
// add custom xml headers
4749
//jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4850

49-
//jaxbMarshaller.marshal(customer, file);
51+
//jaxbMarshaller.marshal(createCompanyObject(), new File("C:\\test\\company.xml"));
52+
5053
jaxbMarshaller.marshal(createCompanyObject(), System.out);
5154

55+
// XML Unmarshalling
56+
/*File file = new File("C:\\test\\company.xml");
57+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
58+
Company o = (Company) jaxbUnmarshaller.unmarshal(file);
59+
System.out.println(o);*/
60+
5261
} catch (JAXBException e) {
5362
e.printStackTrace();
5463
}

java-xml/src/main/java/com/mkyong/xml/jaxb/JaxbExampleFruit.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import jakarta.xml.bind.JAXBContext;
55
import jakarta.xml.bind.JAXBException;
66
import jakarta.xml.bind.Marshaller;
7+
import jakarta.xml.bind.Unmarshaller;
8+
import java.io.File;
79

810
// http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
911
public class JaxbExampleFruit {
@@ -16,24 +18,39 @@ public static void main(String[] args) {
1618
// Normal JAXB RI
1719
//jaxbContext = JAXBContext.newInstance(Fruit.class);
1820

19-
// EclipseLink MOXy jaxb.properties need same package with the Fruit.class
20-
// Alternative, define this via eclipse JAXBContextFactory manually.
21+
// EclipseLink MOXy needs jaxb.properties at the same package with Fruit.class
22+
// Alternative, I prefer define this via eclipse JAXBContextFactory manually.
2123
jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
22-
.createContext(new Class[] {Fruit.class}, null);
24+
.createContext(new Class[]{Fruit.class}, null);
2325

2426
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
2527

2628
// output pretty printed
2729
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
2830

31+
// change XML encoding
32+
// jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
33+
34+
// remove <?xml version="1.0" encoding="UTF-8"?>
35+
//jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
36+
2937
Fruit o = new Fruit();
3038
o.setId(1);
3139
o.setName("Banana");
3240
o.setPrice("9.99");
3341

34-
//jaxbMarshaller.marshal(customer, file);
42+
// output to a xml file
43+
//jaxbMarshaller.marshal(o, new File("C:\\test\\fruit.xml"));
44+
45+
// output to console
3546
jaxbMarshaller.marshal(o, System.out);
3647

48+
// XML Unmarshalling
49+
/*File file = new File("C:\\test\\fruit.xml");
50+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
51+
Fruit o = (Fruit) jaxbUnmarshaller.unmarshal(file);
52+
System.out.println(o);*/
53+
3754
} catch (JAXBException e) {
3855
e.printStackTrace();
3956
}

java-xml/src/main/java/com/mkyong/xml/jaxb/adaptor/TimeZoneAdaptor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
// @Since 3.0.0
44
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
5-
65
//import javax.xml.bind.annotation.adapters.XmlAdapter;
76

87
import java.time.ZonedDateTime;

java-xml/src/main/java/com/mkyong/xml/jaxb/model/Company.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@XmlRootElement
1111
@XmlType(propOrder = {"name", "list"})
12-
// this tells jaxb impl only takes fields for mapping
12+
// this tells jaxb impl only takes fields for mapping
1313
// by default, jaxb impl takes get/set pairs, public fields, and annotated non public fields as mapped, if we annotate the field, we get duplicated error
1414
@XmlAccessorType(XmlAccessType.FIELD)
1515
public class Company {
@@ -39,4 +39,11 @@ public void setList(List<Staff> list) {
3939
this.list = list;
4040
}
4141

42+
@Override
43+
public String toString() {
44+
return "Company{" +
45+
"list=" + list +
46+
", name='" + name + '\'' +
47+
'}';
48+
}
4249
}

java-xml/src/main/java/com/mkyong/xml/jaxb/model/Fruit.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.mkyong.xml.jaxb.model;
22

3-
import jakarta.xml.bind.annotation.*;
3+
import jakarta.xml.bind.annotation.XmlAccessType;
4+
import jakarta.xml.bind.annotation.XmlAccessorType;
5+
import jakarta.xml.bind.annotation.XmlAttribute;
6+
import jakarta.xml.bind.annotation.XmlElement;
7+
import jakarta.xml.bind.annotation.XmlRootElement;
8+
import jakarta.xml.bind.annotation.XmlType;
49

510
@XmlRootElement
611
// order of the fields in XML
7-
// @XmlType(propOrder = {"price", "name"})
12+
@XmlType(propOrder = {"price", "name"})
13+
14+
// this tells jaxb impl only takes fields for mapping
15+
// by default, jaxb impl takes get/set pairs, public fields, and annotated non public fields as mapped, if we annotate the field, we get duplicated error
816
@XmlAccessorType(XmlAccessType.FIELD)
917
public class Fruit {
1018

java-xml/src/main/java/com/mkyong/xml/jaxb/model/Staff.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
// Java 8?
1414
//import com.sun.xml.internal.txw2.annotation.XmlCDATA;
15-
/*import javax.xml.bind.annotation.*;
16-
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;*/
15+
//import javax.xml.bind.annotation.*;
1716
import java.time.ZonedDateTime;
1817

19-
// optional, order of the fields write to XML
2018
@XmlRootElement
2119
@XmlAccessorType(XmlAccessType.FIELD)
2220
public class Staff {
@@ -69,4 +67,15 @@ public ZonedDateTime getJoinDate() {
6967
public void setJoinDate(ZonedDateTime joinDate) {
7068
this.joinDate = joinDate;
7169
}
70+
71+
@Override
72+
public String toString() {
73+
return "Staff{" +
74+
"id=" + id +
75+
", name='" + name + '\'' +
76+
", Salary='" + Salary + '\'' +
77+
", bio='" + bio + '\'' +
78+
", joinDate=" + joinDate +
79+
'}';
80+
}
7281
}

0 commit comments

Comments
 (0)