JAXB

Koleksi Jawa
tingkat , wulangan
kasedhiya

"Ora cepet, bocah enom! Aku duwe rong pelajaran maneh kanggo sampeyan!"

"Loro? Wah. Inggih, oke. Apa sampeyan ora bakal nindakake kanggo nambah coolness dhewe! Aku kabeh kuping."

"XML, kaya JSON, asring digunakake nalika ngirim data ing antarane program lan komputer sing beda-beda. Lan ana sawetara kerangka kerja sing nyederhanakake urip programer Java nalika nggarap XML. Dina iki aku bakal ngenalake sampeyan menyang salah sijine."

"JAXB minangka kerangka multiguna sing apik kanggo nggarap XML."

JAXB - 1

"JAXB minangka bagéan saka JDK, dadi sampeyan ora perlu ngundhuh kanthi kapisah."

"Ayo kula nuduhake sampeyan conto carane nggunakake, banjur kita bakal analisa. Contone:"

Ngonversi obyek menyang XML
public static void main(String[] args) throws JAXBException
{
 // Create an object to be serialized into XML
 Cat cat = new Cat();
 cat.name = "Missy";
 cat.age = 5;
 cat.weight = 4;

 // Write the result of the serialization to a StringWriter
 StringWriter writer = new StringWriter();

 // Create a Marshaller object that will perform the serialization
 JAXBContext context = JAXBContext.newInstance(Cat.class);
 Marshaller marshaller = context.createMarshaller();
 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
 // And here's the serialization itself:
 marshaller.marshal(cat, writer);

 // Convert everything written to the StringWriter
 String result = writer.toString();
 System.out.println(result);
}
Kelas sing obyek diowahi dadi XML
@XmlType(name = "cat")
@XmlRootElement
public class Cat
{
 public String name;
 public int age;
 public int weight;

 public Cat()
 {
 }
}
Asil serialisasi lan output layar:
<kucing> 
<jeneng> Missy </name> 
<umur> 5 </umur> 
<bobot> 4 </bobot> 
</kucing>

"Kanggo sawetara alasan, baris kanggo baris, kode iki ngelingake aku babagan serialisasi JSON. Anotasi sing padha, nanging JSON nggunakake ObjectMapper, lan iki nggunakake Konteks lan Marshaller."

"Ya, pancen padha banget. Jackson dimodelake sawise JAXB. Nanging JAXB uga disalin saka ngendi wae. Sampeyan ora bisa nggawe genius saka awal."

"Koyone kaya ngono."

"Oke, iki kedadeyan:"

"Ing baris 4-7, kita nggawe obyek Cat lan isi karo data."

"Ing baris 10, kita nggawe obyek Writer kanggo nulis asil."

"Ing baris 13, kita nggawe 'konteks'. Iki padha karo ObjectMapper, nanging banjur rong obyek anak tambahan digawe saka iku. Marshaller kanggo serialization, lan Unmarshaller kanggo deserialization. Ana beda cilik saka Jackson, nanging ora. ora beda dhasar."

"Ing baris 14, kita nggawe obyek Marshaller . Marshalling minangka sinonim kanggo serialisasi."

"Ing baris 15, kita nyetel properti FORMATTED_OUTPUT kanggo TRUE. Iki bakal nambah baris break lan spasi kanggo asil, supaya kode bisa diwaca manungsa, lan ora mung kabeh teks ing siji baris."

"Ing baris 17, kita serialize obyek."

"Ing baris 20-21, kita nampilake asil serialisasi ing layar."

"Apa @XmlType(name = 'cat&') lan @XmlRootElement anotasi?"

" @XmlRootElement nuduhake yen obyek iki bisa dadi 'root saka wit' unsur XML. Ing tembung liyane, bisa dadi unsur tingkat paling dhuwur, lan bisa ngemot kabeh unsur liyane."

" @XmlType(name = 'cat') nuduhake yen kelas kasebut melu serialisasi JAXB, lan uga nemtokake jeneng tag XML kanggo kelas iki."

"Oke, saiki aku bakal nuduhake conto deseralisasi XML:"

Ngonversi obyek saka XML
public static void main(String[] args) throws JAXBException
{
 String xmldata = "<cat><name>Missy</name><age>5</age><weight>4</weight></cat>";
 StringReader reader = new StringReader(xmldata);

 JAXBContext context = JAXBContext.newInstance(Cat.class);
 Unmarshaller unmarshaller = context.createUnmarshaller();

 Cat cat = (Cat) unmarshaller.unmarshal(reader);
}
A kelas kang obyek deserialized saka XML
@XmlType(name = "cat")
@XmlRootElement
public class Cat
{
 public String name;
 public int age;
 public int weight;

 public Cat()
 {
 }
}

"Kabeh meh padha karo Jackson. Nanging yen ngono, aku bakal nerangake kabeh sing kedadeyan ing kene."

"Ing baris 3, kita ngliwati senar sing nyimpen XML kanggo deserialized."

"Ing baris 4, kita mbungkus senar XML ing StringReader ."

"Ing baris 6, kita nggawe konteks JAXB , sing kita pass dhaptar kelas."

"Ing baris 7, kita nggawe Unmarshaller - obyek sing bakal nindakake deserialization."

"Ing baris 9, kita deserialize XML saka obyek maca lan njaluk obyek Cat."

"Saiki kabeh katon meh ketok. Nanging sawetara jam kepungkur, aku ngetung otak kanggo ngerti cara kerjane."

"Iku mesthi kedadeyan yen sampeyan luwih pinter. Perkara sing rumit dadi prasaja."

"Aku tambah pinter? Aku ora bisa apa nanging seneng bab iku."

"Apik. Banjur iki dhaptar anotasi sing bisa digunakake kanggo ngontrol asil serialisasi JAXB: "

anotasi JAXB Katrangan
@XmlElement(jeneng) Diselehake ing cedhak sawah.
Lapangan bakal disimpen ing unsur XML.
Ngidini sampeyan nyetel jeneng tag.
@XmlAttribute(jeneng) Diselehake ing cedhak sawah.
Kolom bakal disimpen minangka atribut XML!
Ngidini sampeyan nyetel jeneng atribut.
@XmlElementWrapper(nillable = bener) Diselehake ing cedhak sawah.
Ngidini sampeyan nyetel 'tag wrapper' kanggo klompok unsur.
@XmlType Diselehake ing cedhak kelas.
Ngidini sampeyan nemtokake cara kanggo nggawe obyek yen konstruktor standar pribadi.
@XmlJavaTypeAdapter Diselehake ing cedhak sawah.
Ngidini sampeyan nemtokake kelas sing bakal ngowahi lapangan dadi senar.

"Carane menarik! Nanging sampeyan bisa menehi kula conto sing nggunakake anotasi iki? Panjelasan iku siji bab, nanging conto sing bisa digunakake liyane."

"OK. Aku bakal nuduhake sampeyan conto. Aku mung pengin nambah sing JAXB ngijini sampeyan annotate getter / cara setter tinimbang lapangan ".

"Iki conto sing aku janji:"

Ngonversi obyek menyang XML
public static void main(String[] args) throws JAXBException
{
 // Create Cat and Zoo objects to be serialized into XML
 Cat cat = new Cat();
 cat.name = "Missy";
 cat.age = 5;
 cat.weight = 4;

 Zoo zoo = new Zoo();
 zoo.animals.add(cat);
 zoo.animals.add(cat);

 // Write the result of the serialization to a StringWriter
 StringWriter writer = new StringWriter();

 // Create a Marshaller object that will perform the serialization
 JAXBContext context = JAXBContext.newInstance(Cat.class, Zoo.class);
 Marshaller marshaller = context.createMarshaller();
 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
 // Here's the serialization itself
 marshaller.marshal(zoo, writer);

 // Convert everything written to the StringWriter into a String
 System.out.println(writer.toString());
}
Kelas sing obyek diowahi dadi XML
@XmlType(name = "zoo")
@XmlRootElement
public class Zoo
{
 @XmlElementWrapper(name = "wild-animals", nillable = true)
 @XmlElement(name = "tiger")
 public List<Cat> animals = new ArrayList<>();
}

public class Cat
{
 @XmlElement(name = "catname")
 public String name;
 @XmlAttribute(name = "age")
 public int age;
 @XmlAttribute(name = "w")
 public int weight;

 public Cat()
 {
 }
}
Asil serialisasi lan output layar:
<kebun 
binatang> 
<kewan liar> <umur macan = "5" w = "4"> 
<jeneng kucing>Cewek</name> 
</macan> 
<umur macan = "5" w = "4"> 
<jeneng kucing>Missy </jeneng kucing 
> </macan> 
</kewan liar> 
</zoo>

"Elinga yen wektu iki kita ora nggawe serial obyek Kucing, nanging obyek Zoo sing nyimpen koleksi obyek Kucing."

"Obyek kucing ditambahake menyang koleksi kaping pindho, dadi ing XML kaping pindho."

"Koleksi kasebut duwe tag ' kewan liar ' dhewe sing mbungkus kabeh unsur koleksi."

" Unsur umur & bobot wis dadi atribut umur & w ."

"Nganggo atribut @XmlElement , kita ngganti tag kucing dadi tag macan ."

"Nggatekake baris 17. Ing kene kita ngliwati rong kelas ( Kebun Binatang lan Kucing ) menyang konteks JAXB, amarga padha melu serialisasi."

"Dina iki dina sing menarik banget. Informasi anyar akeh banget."

"Ya, aku seneng karo sampeyan. Saiki, istirahat sedhela lan kita bakal nerusake."

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION