Geven XML file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ExternalRequestContext [
<!ELEMENT ExternalRequestContext EMPTY>
<!ATTLIST ExternalRequestContext
requestType CDATA #REQUIRED
deepEnrichment (true | false) "false"
channelMandatory (true | false) "true">
]
>
<ExternalRequestContext requestType="News" deepEnrichment="false" />
And xStream code
@XStreamAlias("ExternalRequestContext")
class ERC {
private String requestType;
private boolean deepEnrichment;
private boolean channelMandatory;
}
...
XStream x = new XStream();
x.processAnnotations(ERC.class);
ERC erc = (ERC)x.fromXML(new FileReader("C:/Projects/Forwarder/Test.xml"));
x.toXML(erc, System.out);
My browser renders it as following:
<ExternalRequestContext requestType="News" deepEnrichment="false" channelMandatory="true" />
Note that channelMandatory=”true” (browser processed the DTD instruction)
while xStream produces
<ExternalRequestContext>
<deepEnrichment>false</deepEnrichment>
<channelMandatory>false</channelMandatory>
</ExternalRequestContext>
Here channelMandatory=”false” (xStream ignored the “channelMandatory (true | false) “true”” DTD instruction)
What do I miss? How to “tell” xStream to process DTD instructions?
And how do I enable DTD validation in xStream?
This could be because you’re using the primitive
booleantype. When classERCis instantiated, thechannelMandatoryfield is initialized by java tofalse. Since the document contains no data for that field, it stays atfalse.DTD validation in java is just that – validation. It doesn’t modify the document, it leaves it as it was, it just permits
channelMandatoryto be not present, knowing that it has a default value. If a web browser chooses to do other wise, that’s fine, but that’s going beyond validation.You could try the simplest potential solution – initialise the
channelMandatoryfield totrue, e.g.That would probably work fine. This is the approach that JAXB takes for generating a java object model from a schema, I think.