xStream 中 DTD 的默认值

发布于 2024-08-17 18:36:45 字数 1378 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-08-24 18:36:45

这可能是因为您使用的是原始 boolean 类型。当类 ERC 实例化时,channelMandatory 字段由 java 初始化为 false。由于文档不包含该字段的数据,因此它保持为 false

java 中的 DTD 验证就是这样——验证。它不会修改文档,而是保持原样,它只是允许 channelMandatory 不存在,因为它知道它有一个默认值。如果网络浏览器选择采取其他方式,那很好,但这超出了验证范围。

您可以尝试最简单的潜在解决方案 - 将 channelMandatory 字段初始化为 true,例如,

@XStreamAlias("ExternalRequestContext")
class ERC {
 private String requestType;
 private boolean deepEnrichment = false;
 private boolean channelMandatory = true;
}

这可能会很好地工作。我认为,这就是 JAXB 从模式生成 java 对象模型所采用的方法。

This could be because you're using the primitive boolean type. When class ERC is instantiated, the channelMandatory field is initialized by java to false. Since the document contains no data for that field, it stays at false.

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 to true, e.g.

@XStreamAlias("ExternalRequestContext")
class ERC {
 private String requestType;
 private boolean deepEnrichment = false;
 private boolean channelMandatory = true;
}

That would probably work fine. This is the approach that JAXB takes for generating a java object model from a schema, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文