Tôi đang sử dụng JAXB để chuyển đổi mô hình miền của mình sang các biểu diễn XML và JSON. Tôi có pojo Sinh viên để chuyển đổi thành XMl/JSON. Nó có thuộc tính content
có thể thuộc bất kỳ loại dữ liệu nào.Xóa xsi: nhập thông tin từ xml/json JAXB?
nét Schema cho nó:
<xs:element name="content" type="xs:anyType" />
Như vậy file java tạo có Object
loại cho nội dung.
Student.java:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "student")
public class Student
extends People
{
................
@XmlElement(required = true)
protected Object content;
}
tôi marshall sử dụng đoạn mã sau:
Marshall:
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "name-binding.xml");
this.ctx = JAXBContext.newInstance("packagename",
packagename.ObjectFactory.class.getClassLoader(), properties);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE,media-type);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT,true);
marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, true);
StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
XML:
<student>
<name>Jack n Jones</name>
<content xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">Sid</content>
</student>
xmlns:xsi
và xsi:type="xsd:string">
sẽ được thêm vào trong phần tử nội dung. Tôi không muốn thông tin kiểu này trong XML của tôi.
Tương tự như vậy cho JSON nó cho biết thêm các thông tin loại:
JSON:
{
"name" : "Jack n Jones",
"content" : {
"type" : "string",
"value" : "Sid"
}
}
Làm thế nào tôi có thể loại bỏ các loại thông tin và tạo ra XML/JSON theo kiểu của nó lúc chạy. Vì vậy, bất cứ loại là content
nó có được của chuyển đổi sang loại không có loại thông tin
Ví dụ, nếu nội dung là String
sau đó XML:
<student>
<name>Jack n Jones</name>
<content>Sid</content>
</student>