Apache Xmlbeans for databinding (XML and POJO)

เช่นเคยครับสร้าง Project ด้วย Maven นะครับ

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>zengcode.medium.com</groupId>
<artifactId>spring-boot-xmlbeans</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
<scope>compile</scope>
</dependency>

</dependencies>

<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id />
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/xsd</schemaDirectory>
<staleFile>${project.build.directory}/generated-sources/xmlbeans/.staleFlag</staleFile>
<verbose>false</verbose>
<quiet>false</quiet>
<javaSource>1.6</javaSource>
</configuration>
</plugin>
</plugins>
</build>
</project>
ผมแอบไปเอา XSD มาจาก http://xmlbeans.apache.org/documentation/tutorial_getstarted.html
ผมสร้างไฟล์ esypo.xsd ไปไว้ใน src/main/xsd แล้วก็ใช้คำสั่ง 
mvn clean install

XmlBeans ก็จะสร้าง POJO จาก XSD ที่ผมสร้างขึ้นให้ ต่อไปเรามาทดสอบโหลด xml string เข้าไปให้ POJO กันเลยครับ

package zengcode.medium.com;

import org.openuri.easypo.PurchaseOrderDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zengcode.medium.com.service.LocationService;

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private LocationService locationService;

@Override
public void run(String... args) throws Exception {


String xml = "<purchase-order xmlns=\"http://openuri.org/easypo\">\n" +
" <customer>\n" +
" <name>Gladys Kravitz</name>\n" +
" <address>Anytown, PA</address>\n" +
" </customer>\n" +
" <date>2003-01-07T14:16:00-05:00</date>\n" +
" <line-item>\n" +
" <description>Burnham's Celestial Handbook, Vol 1</description>\n" +
" <per-unit-ounces>5</per-unit-ounces>\n" +
" <price>21.79</price>\n" +
" <quantity>2</quantity>\n" +
" </line-item>\n" +
" <line-item>\n" +
" <description>Burnham's Celestial Handbook, Vol 2</description>\n" +
" <per-unit-ounces>5</per-unit-ounces>\n" +
" <price>19.89</price>\n" +
" <quantity>2</quantity>\n" +
" </line-item>\n" +
" <shipper>\n" +
" <name>ZipShip</name>\n" +
" <per-ounce-rate>0.74</per-ounce-rate>\n" +
" </shipper>\n" +
"</purchase-order>";


PurchaseOrderDocument poDoc = PurchaseOrderDocument.Factory.parse(xml);

System.out.println("--->>> " + poDoc.getPurchaseOrder().getCustomer().getName());

}

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

}

เมื่อผมรัน Application มันก็จะพิมพ์

— ->>> Gladys Kravitz

เห็นไหมครับว่ามันง่ายนิดเดียว จริงๆ

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.