xStream 中 DTD 的默认值
Geven XML 文件
<?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" />
和 xStream 代码
@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);
我的浏览器将其呈现如下:
<ExternalRequestContext requestType="News" deepEnrichment="false" channelMandatory="true" />
请注意,channelMandatory="true"(浏览器处理了 DTD 指令),
而 xStream 生成
<ExternalRequestContext>
<deepEnrichment>false</deepEnrichment>
<channelMandatory>false</channelMandatory>
</ExternalRequestContext>
此处 channelMandatory="false" (xStream 忽略了“channelMandatory (true | false) “true””DTD 指令)
我错过了什么?如何“告诉”xStream处理DTD指令? 如何在 xStream 中启用 DTD 验证?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您使用的是原始
boolean
类型。当类ERC
实例化时,channelMandatory
字段由 java 初始化为false
。由于文档不包含该字段的数据,因此它保持为false
。java 中的 DTD 验证就是这样——验证。它不会修改文档,而是保持原样,它只是允许
channelMandatory
不存在,因为它知道它有一个默认值。如果网络浏览器选择采取其他方式,那很好,但这超出了验证范围。您可以尝试最简单的潜在解决方案 - 将
channelMandatory
字段初始化为true
,例如,这可能会很好地工作。我认为,这就是 JAXB 从模式生成 java 对象模型所采用的方法。
This could be because you're using the primitive
boolean
type. When classERC
is instantiated, thechannelMandatory
field 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
channelMandatory
to 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
channelMandatory
field 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.