आप सीधे JAXBElement
का उदाहरण बना सकते हैं या यदि आपने एक्सएमएल स्कीमा से अपना जावा मॉडल जेनरेट किया है तो उत्पन्न ObjectFactory
कक्षा पर एक रूपांतरण विधि का उपयोग करें।
package org.example.schema;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("org.example.schema");
Root root = new Root();
QName fooQName = new QName("http://www.example.org/schema", "foo");
JAXBElement<String> fooValue = new JAXBElement<String>(fooQName, String.class, "FOO");
root.setFoo(fooValue);
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<String> barValue = objectFactory.createRootBar("BAR");
root.setBar(barValue);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
schema.xsd
ऊपर डेमो कोड निम्नलिखित XML स्कीमा से उत्पन्न एक जावा मॉडल पर आधारित है। पहली बार JAXBElement<String>
संपत्ति प्राप्त करने का कारण यह है कि जब आपके पास कोई तत्व है जो nillable="true"
और minOccurs="0"
दोनों है।
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns:tns="http://www.example.org/schema"
elementFormDefault="qualified">
<element name="root">
<complexType>
<sequence>
<element name="foo" type="string" minOccurs="0" nillable="true"/>
<element name="bar" type="string" minOccurs="0" nillable="true"/>
</sequence>
</complexType>
</element>
</schema>
रूट
निम्नलिखित वर्ग schema.xsd
से उत्पन्न और अपने प्रश्न में से एक की तरह गुण होता था।
package org.example.schema;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"foo","bar"})
@XmlRootElement(name = "root")
public class Root {
@XmlElementRef(name = "foo", namespace = "http://www.example.org/schema", type = JAXBElement.class)
protected JAXBElement<String> foo;
@XmlElementRef(name = "bar", namespace = "http://www.example.org/schema", type = JAXBElement.class)
protected JAXBElement<String> bar;
public JAXBElement<String> getFoo() {
return foo;
}
public void setFoo(JAXBElement<String> value) {
this.foo = value;
}
public JAXBElement<String> getBar() {
return bar;
}
public void setBar(JAXBElement<String> value) {
this.bar = value;
}
}
ObjectFactory
नीचे उत्पन्न ObjectFactory
वर्ग कि JAXBElement
के उदाहरण बनाने के लिए सुविधा के तरीकों में शामिल है।
package org.example.schema;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _RootFoo_QNAME = new QName("http://www.example.org/schema", "foo");
private final static QName _RootBar_QNAME = new QName("http://www.example.org/schema", "bar");
public Root createRoot() {
return new Root();
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "foo", scope = Root.class)
public JAXBElement<String> createRootFoo(String value) {
return new JAXBElement<String>(_RootFoo_QNAME, String.class, Root.class, value);
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "bar", scope = Root.class)
public JAXBElement<String> createRootBar(String value) {
return new JAXBElement<String>(_RootBar_QNAME, String.class, Root.class, value);
}
}
की [वेब सेवा में JAXBElement का उपयोग कैसे करें?] (Http://stackoverflow.com/questions/1393920/how-to-use-jaxbelementstring-in-web-service) –
Vadzim