BAEL-707 Introduction to JiBX (#1340)
This commit is contained in:
53
xml/src/main/java/com/baeldung/xml/jibx/Customer.java
Normal file
53
xml/src/main/java/com/baeldung/xml/jibx/Customer.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Customer.java 06.06.2008
|
||||
*
|
||||
* Copyright 2008 Stefan Jäger
|
||||
*
|
||||
*/
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
|
||||
public class Customer {
|
||||
private Person person;
|
||||
private String city;
|
||||
|
||||
private Phone homePhone;
|
||||
private Phone officePhone;
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public Phone getHomePhone() {
|
||||
return homePhone;
|
||||
}
|
||||
|
||||
public void setHomePhone(Phone homePhone) {
|
||||
this.homePhone = homePhone;
|
||||
}
|
||||
|
||||
public Phone getOfficePhone() {
|
||||
return officePhone;
|
||||
}
|
||||
|
||||
public void setOfficePhone(Phone officePhone) {
|
||||
this.officePhone = officePhone;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
||||
14
xml/src/main/java/com/baeldung/xml/jibx/Identity.java
Normal file
14
xml/src/main/java/com/baeldung/xml/jibx/Identity.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
public class Identity {
|
||||
|
||||
long customerId;
|
||||
|
||||
public long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
}
|
||||
34
xml/src/main/java/com/baeldung/xml/jibx/Person.java
Normal file
34
xml/src/main/java/com/baeldung/xml/jibx/Person.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Person.java 06.06.2008
|
||||
*
|
||||
* Copyright 2008 Stefan Jäger
|
||||
*
|
||||
*/
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
|
||||
public class Person extends Identity {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
}
|
||||
}
|
||||
32
xml/src/main/java/com/baeldung/xml/jibx/Phone.java
Normal file
32
xml/src/main/java/com/baeldung/xml/jibx/Phone.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
public class Phone {
|
||||
|
||||
private String countryCode;
|
||||
private String networkPrefix;
|
||||
private String number;
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
public void setCountryCode(String countryCode) {
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
public String getNetworkPrefix() {
|
||||
return networkPrefix;
|
||||
}
|
||||
|
||||
public void setNetworkPrefix(String networkPrefix) {
|
||||
this.networkPrefix = networkPrefix;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
49
xml/src/main/resources/Order.xsd
Normal file
49
xml/src/main/resources/Order.xsd
Normal file
@@ -0,0 +1,49 @@
|
||||
<xs:schema attributeFormDefault="unqualified"
|
||||
elementFormDefault="qualified" targetNamespace="http://www.jibx.xml.baeldung.com/"
|
||||
xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="Order">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Address" maxOccurs="unbounded"
|
||||
minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element type="xs:string" name="Name" />
|
||||
<xs:element type="xs:string" name="Street" />
|
||||
<xs:element type="xs:string" name="City" />
|
||||
<xs:element type="xs:string" name="State" />
|
||||
<xs:element type="xs:int" name="Zip" />
|
||||
<xs:element type="xs:string" name="Country" />
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="Type" use="optional" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
<xs:element type="xs:string" name="DeliveryNotes" />
|
||||
<xs:element name="Items">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Item" maxOccurs="unbounded"
|
||||
minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element type="xs:string" name="ProductName" />
|
||||
<xs:element type="xs:byte" name="Quantity" />
|
||||
<xs:element type="xs:float" name="USPrice" />
|
||||
<xs:element type="xs:string" name="Comment"
|
||||
minOccurs="0" />
|
||||
<xs:element type="xs:date" name="ShipDate"
|
||||
minOccurs="0" />
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="PartNumber"
|
||||
use="optional" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:int" name="OrderNumber" />
|
||||
<xs:attribute type="xs:date" name="OrderDate" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
29
xml/src/main/resources/customer-binding.xml
Executable file
29
xml/src/main/resources/customer-binding.xml
Executable file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding>
|
||||
<mapping class="com.baeldung.xml.jibx.Identity" abstract="true">
|
||||
<value name="customer-id" field="customerId" />
|
||||
</mapping>
|
||||
|
||||
<mapping name="customer" class="com.baeldung.xml.jibx.Customer">
|
||||
<structure field="person" />
|
||||
<structure name="home-phone" field="homePhone" usage="optional" />
|
||||
<structure name="office-phone" field="officePhone" usage="optional" />
|
||||
<value name="city" field="city" />
|
||||
</mapping>
|
||||
|
||||
<mapping name="person" class="com.baeldung.xml.jibx.Person"
|
||||
extends="com.baeldung.xml.jibx.Identity">
|
||||
<structure map-as="com.baeldung.xml.jibx.Identity" />
|
||||
<value name="first-name" field="firstName" />
|
||||
<value name="last-name" field="lastName" />
|
||||
|
||||
</mapping>
|
||||
|
||||
<mapping class="com.baeldung.xml.jibx.Phone" abstract="true">
|
||||
<value name="country-code" field="countryCode" />
|
||||
<value name="network-prefix" field="networkPrefix" />
|
||||
<value name="number" field="number" />
|
||||
</mapping>
|
||||
|
||||
|
||||
</binding>
|
||||
56
xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java
Normal file
56
xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
import org.jibx.runtime.BindingDirectory;
|
||||
import org.jibx.runtime.IBindingFactory;
|
||||
import org.jibx.runtime.IUnmarshallingContext;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CustomerTest {
|
||||
|
||||
@Test
|
||||
public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("Stefan", customer.getPerson().getFirstName());
|
||||
assertEquals("Jaeger", customer.getPerson().getLastName());
|
||||
assertEquals("Davos Dorf", customer.getCity());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenMappingInherited() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals(12345, customer.getPerson().getCustomerId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("1", customer.getHomePhone().getCountryCode());
|
||||
assertEquals("234", customer.getHomePhone().getNetworkPrefix());
|
||||
assertEquals("234678", customer.getHomePhone().getNumber());
|
||||
|
||||
}
|
||||
}
|
||||
21
xml/src/test/resources/Customer1.xml
Normal file
21
xml/src/test/resources/Customer1.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<customer>
|
||||
<person>
|
||||
<customer-id>12345</customer-id>
|
||||
<first-name>Stefan</first-name>
|
||||
<last-name>Jaeger</last-name>
|
||||
</person>
|
||||
<home-phone>
|
||||
<country-code>1</country-code>
|
||||
<network-prefix>234</network-prefix>
|
||||
<number>234678</number>
|
||||
</home-phone>
|
||||
|
||||
<office-phone>
|
||||
<country-code>1</country-code>
|
||||
<network-prefix>234</network-prefix>
|
||||
<number>234678</number>
|
||||
</office-phone>
|
||||
<city>Davos Dorf</city>
|
||||
|
||||
</customer>
|
||||
Reference in New Issue
Block a user