मैं अपने डोमेन-मॉडल को एक्सएमएल और जेएसओएन प्रस्तुतियों में परिवर्तित करने के लिए जेएक्सबी का उपयोग कर रहा हूं। मेरे पास XMl/JSON में कनवर्ट करने के लिए छात्र pojo है। इसमें 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;
}
मैं मार्शल निम्नलिखित कोड का उपयोग कर:
मार्शल:
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);
एक्सएमएल:
<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">
सामग्री तत्व में शामिल हो रहे हैं। मैं इस प्रकार की जानकारी को अपने एक्सएमएल में नहीं चाहता हूं।
JSON के लिए इसी तरह यह प्रकार की जानकारी को जोड़ता है:
JSON:
{
"name" : "Jack n Jones",
"content" : {
"type" : "string",
"value" : "Sid"
}
}
मैं प्रकार जानकारी को कैसे निकाल और रन टाइम पर यह प्रकार के अनुसार XML/JSON उत्पन्न कर सकते हैं। तो जो कुछ भी प्रकार है content
यह प्रकार की जानकारी
बिना प्रकार करने के लिए परिवर्तित उदाहरण के लिए मिलता है सामग्री String
फिर XML है:
<student>
<name>Jack n Jones</name>
<content>Sid</content>
</student>