Lưu ý: Tôi là người lãnh đạo EclipseLink JAXB (MOXy) và là thành viên của chuyên gia JAXB (JSR-222) nhóm,
Dưới đây là cách thực hiện việc này nếu bạn đang sử dụng MOXy làm nhà cung cấp JAXB của mình.
JAVA MODEL
khách hàng
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement(namespace="http://www.example.com")
@XmlType(namespace="http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private int id;
@XmlElement(namespace="http://www.example.com")
private String firstName;
@XmlElement(namespace="http://www.example.com", nillable=true)
private String lastName;
@XmlElement(namespace="http://www.example.com")
private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
}
PhoneNumber
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
@XmlAttribute
private String type;
@XmlValue
private String number;
}
jaxb.properties
Để xác định MOXY là nhà cung cấp JAXB của bạn, bạn cần phải bao gồm một tập tin gọi jaxb.properties
trong gói tương tự như mô hình tên miền của bạn với các mục sau đây (xem: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
DEMO MÃ
i nput.xml
<?xml version="1.0" encoding="UTF-8"?>
<ns0:customer xmlns:ns0="http://www.example.com" id="123">
<ns0:firstName>Jane</ns0:firstName>
<ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ns0:phoneNumbers type="work">555-1111</ns0:phoneNumbers>
</ns0:customer>
Demo
Trong code demo dưới đây chúng tôi sẽ sử dụng cùng một siêu dữ liệu JAXB để chuyển đổi một tài liệu XML để các đối tượng Java, và sau đó chuyển đổi các đối tượng trở về JSON. Với MOXy, bạn có thể chỉ định đầu ra JSON bằng cách đặt thuộc tính trên Marshaller
.
import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum15357366/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml)
;
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.marshal(customer, System.out);
}
}
JSON Output
Dưới đây là sản phẩm JSON. Lưu ý cách không có chỉ báo nào tương ứng với không gian tên hoặc thuộc tính XML. Cũng lưu ý rằng tập hợp kích thước một được biểu diễn chính xác như một mảng JSON (một vấn đề với một số cách tiếp cận khác).
{
"id" : 123,
"firstName" : "Jane",
"lastName" : null,
"phoneNumbers" : [ {
"type" : "work",
"value" : "555-1111"
} ]
}
@BDoughan: Có Tôi cũng đã thấy điều này trong blog của bạn. Nhưng tôi nhận được ClassNotFound ngoại lệ trên JAXBContextFactory.I đã bao gồm các jaxb.properties và tôi đặt một phụ thuộc trong pom.xml quá. vui lòng hướng dẫn tôi về sự phụ thuộc chính xác cho lớp đó – Arun
@ user1933756 - Đây là một tệp 'pom.xml' từ một ví dụ mà tôi đã lưu trữ trên GitHub mà bạn có thể sử dụng làm hướng dẫn: https://github.com/bdoughan/blog20110819 /blob/master/pom.xml –
Cảm ơn một tấn Doughan :) hoạt động tuyệt vời !!! – Arun