發現這比我製作它要容易得多,或者比隱含的鏈接問題容易得多。我被掛在了沒有@XmlElementRoot
的事實上,但事實證明這是沒有必要的。
simple.xsd
:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="receive" nillable="true" />
</xs:schema>
在命令行上,然.../jdk1.8/bin/xjc simple.xsd
即生成ObjectFactory
。 ObjectFactory
中的一種方法的簽名爲public JAXBElement<Object> createReceive(Object value)
。
創建下面的實現代碼:
package aTest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import generated.ObjectFactory;
public class start {
public static void main(final String[] args) {
final ObjectFactory factory = new ObjectFactory();
final JAXBElement<Object> element = factory.createReceive(null);
try {
final JAXBContext jaxbContext = JAXBContext.newInstance(JAXBElement.class);
final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(element, System.out);
} catch (final JAXBException e) {
e.printStackTrace();
}
}
}
這產生所期望的輸出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<receive xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>