JAXBを使用して、ドメインモデルをXMLおよびJSON表現に変換しています。 私はXMl/JSONに変換するためのStudent pojoを持っています。それは任意のデータ型の可能性があるcontent
プロパティを持っています。そのためxsi:xml/json JAXBから情報を削除しますか?
スキーマ定義:
<xs:element name="content" type="xs:anyType" />
このように生成されたJavaファイルは、コンテンツのObject
タイプがあります。
Student.java:
マーシャル:
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:
次のコードを使用して@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "student")
public class Student
extends People
{
................
@XmlElement(required = true)
protected Object content;
}
Iマーシャルコンテンツ要素には、
<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">
が追加されています。私はこのタイプの情報を私のXMLには必要としません。
JSON::
{
"name" : "Jack n Jones",
"content" : {
"type" : "string",
"value" : "Sid"
}
}
どのように型情報を削除し、実行時でそれの種類に応じてXML/JSONを生成することができますJSONのための同様
それは型情報を追加します。
<student>
<name>Jack n Jones</name>
<content>Sid</content>
</student>