도메인 모델을 XML 및 JSON 표현으로 변환하는 데 JAXB를 사용하고 있습니다. XMl/JSON으로 변환 할 수있는 학생용 버전이 있습니다. 모든 데이터 형식이 될 수있는 content
속성이 있습니다. 그것을 위해xsi : xml/json JAXB에서 정보를 입력하십시오.
스키마 정의 :
<xs:element name="content" type="xs:anyType" />
는 따라서 자바 파일이 생성 콘텐츠에 대한
Object
유형이 있습니다.
Student.java :
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "student")
public class Student
extends People
{
................
@XmlElement(required = true)
protected Object content;
}
I 마샬 다음 코드를 사용하여 :
마샬 :
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
및 xsi:type="xsd:string">
이 content 요소에 추가됩니다. XML에서이 유형 정보를 원하지 않습니다.
JSON : 나는 형식 정보를 제거하고 런타임에서 그것의 종류에 따라 XML/JSON을 생성 할 수있는 방법
{
"name" : "Jack n Jones",
"content" : {
"type" : "string",
"value" : "Sid"
}
}
.
<student>
<name>Jack n Jones</name>
<content>Sid</content>
</student>