我使用xjc从dtd创建对象。
xjc -dtd mydtd.dtd -d src
但我对默认值感到困惑......
在dtd文件中,我有一个如下元素:
<!ELEMENT ImageOrder (Target, Format, Source, Output, Rules?, VolumeName?, PVDInfo?, Controls?, Customize?)>
<!ATTLIST ImageOrder
OrderId CDATA #REQUIRED
ClientId CDATA #REQUIRED
Priority (Low | Normal | High) "Normal"
StreamerExternal (true | false) "false"
StreamerLogonId CDATA #IMPLIED>
xjc创建如下对象:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"target",
"format",
"source",
"output",
"rules",
"volumeName",
"pvdInfo",
"controls",
"customize"
})
@XmlRootElement(name = "ImageOrder")
public class ImageOrder {
@XmlAttribute(name = "OrderId", required = true)
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected String orderId;
@XmlAttribute(name = "ClientId", required = true)
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
protected String clientId;
@XmlAttribute(name = "Priority")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String priority;
@XmlAttribute(name = "StreamerExternal")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String streamerExternal;
.
.
.
.
.
public String getPriority() {
if (priority == null) {
return "Normal";
} else {
return priority;
}
}
.
.
.
public String getStreamerExternal() {
if (streamerExternal == null) {
return "false";
} else {
return streamerExternal;
}
}
我想编组一个新的ImageOrder实例。 但是当没有设置编组默认值时。 我想用默认值编组。 我怎样才能做到这一点。
ImageOrder imageOrder = new ImageOrder();
imageOrder.setOrderId("orderId");
imageOrder.setClientId("clientId");
JAXBContext context = JAXBContext.newInstance(ImageOrder.class);
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-16");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
marshaller.marshal(obj, stringWriter);
预期xml:
<ImageOrder ClientId="clientId" OrderId="orderId" Priority="Normal" StreamerExternal="false">
</ImageOrder>
但是得到这个:
<ImageOrder ClientId="clientId" OrderId="orderId">
</ImageOrder>