JAXB forcing namespace URI - 为什么它期望某个命名空间,即使它没有被定义?
JAXB forcing namespace URI - why does it expect certain namespace even when it was not defined?
我在尝试解压缩 XML 文件时有一个谜:
意外元素(uri:“http://www.xxx/xsd/ems/batchreq”,本地:"batchParams")。预期的元素是 <{}msgCollection>,<{}batchParams>]
元素不应该带有命名空间 - 它在 xml 中没有,在 java class 中也没有- 对于更大的谜团,具有命名空间的元素似乎并不期望它。
为什么?
向@XmlElement 添加命名空间会有所帮助,但似乎必须为每个元素都定义它 - 这是不可接受的解决方案。为什么命名空间不被子元素继承?
根 class 是:
@XmlRootElement(name = "batch", namespace = "http://www.xxx/xsd/ems/batchreq")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "batch", namespace = "http://www.xxx/xsd/ems/batchreq", propOrder = { "batchParams", "msgCollection" })
public class Batch {
@XmlAttribute(required = true)
private String batchName;
@XmlElement(name = "batchParams", required = true)
private BatchParams batchParams;
@XmlElement(name = "msgCollection", required = true)
private Msgs msgCollection;
...
违规成员 class 是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "batchParams", namespace = "http://www.xxx/xsd/ems/batchreq", propOrder = { "msgCount", "dateCreated" })
public class BatchParams {
@XmlElement()
private Long msgCount;
@XmlElement()
private Date dateCreated;
public Long getMsgCount() {
...
Xml 文档为:
<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns="http://www.xxx/xsd/ems/batchreq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx/xsd/ems/batchreq ./ems_msg_batchreq.xsd" batchName="d" msgType="ems_sendemsg_batchreq" sourceSystem="1">
<batchParams>
<msgCount>1</msgCount>
<dateCreated>2014-12-03T12:00:00</dateCreated>
</batchParams>
<msgCollection xmlns="http://www.xxx/xsd/ems/req">
<msg msgType="ems_sendemsg_msg" sourceSystem="1" dataSourceSystem="1">
...
</msg>
</msgCollection>
</batch>
Element should not come with namespace - it does not have in in xml,
nor in java class, yet it seems that when it is parsed, it gets
namespace uri - and for bigger mystery, elements that have namespace
do not seem to expect it.
您问题中的 XML 将 http://www.xxx/xsd/ems/batchreq
声明为默认命名空间。这意味着 XML 中未分配给不同命名空间的每个元素都将成为该命名空间的一部分。
<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns="http://www.xxx/xsd/ems/batchreq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx/xsd/ems/batchreq ./ems_msg_batchreq.xsd" batchName="d" msgType="ems_sendemsg_batchreq" sourceSystem="1">
<batchParams>
<msgCount>1</msgCount>
<dateCreated>2014-12-03T12:00:00</dateCreated>
</batchParams>
<msgCollection xmlns="http://www.xxx/xsd/ems/req">
<msg msgType="ems_sendemsg_msg" sourceSystem="1" dataSourceSystem="1">
...
</msg>
</msgCollection>
</batch>
Adding namespace to @XmlElement helps, but then it seems that it would
have to be defined for every single element - that is unacceptable
solution.
您可以使用包级别 @XmlSchema
注释来映射默认名称空间限定。这在名为 package-info.java
的源文件中进行,其中仅包含如下所示的内容。您需要更改包名称以匹配您的域模型。
@XmlSchema(
namespace = "http://www.xxx/xsd/ems/batchreq",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Why is namespace not inherited by child elements?
JAXB 中的命名空间规范确实得到了继承,只是不是以您尝试过的方式。
- 包级别
@XmlSchema
注释提供了一种指定默认名称空间的方法,该名称空间应由对应于该包中 classes 和属性的所有元素继承。
- 类型级别
@XmlType
注释提供了一种方法来指定此 class 上所有属性继承的命名空间。这会覆盖 @XmlSchema
注释中指定的命名空间信息。
- 最后,可以在
@XmlRootElement
、@XmlElement
注释(以及其他一些注释)上指定名称空间。此处指定的名称空间仅适用于该元素。
了解更多信息
我在我的博客上写了更多关于这个用例的文章:
我在尝试解压缩 XML 文件时有一个谜:
意外元素(uri:“http://www.xxx/xsd/ems/batchreq”,本地:"batchParams")。预期的元素是 <{}msgCollection>,<{}batchParams>]
元素不应该带有命名空间 - 它在 xml 中没有,在 java class 中也没有- 对于更大的谜团,具有命名空间的元素似乎并不期望它。
为什么?
向@XmlElement 添加命名空间会有所帮助,但似乎必须为每个元素都定义它 - 这是不可接受的解决方案。为什么命名空间不被子元素继承?
根 class 是:
@XmlRootElement(name = "batch", namespace = "http://www.xxx/xsd/ems/batchreq")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "batch", namespace = "http://www.xxx/xsd/ems/batchreq", propOrder = { "batchParams", "msgCollection" })
public class Batch {
@XmlAttribute(required = true)
private String batchName;
@XmlElement(name = "batchParams", required = true)
private BatchParams batchParams;
@XmlElement(name = "msgCollection", required = true)
private Msgs msgCollection;
...
违规成员 class 是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "batchParams", namespace = "http://www.xxx/xsd/ems/batchreq", propOrder = { "msgCount", "dateCreated" })
public class BatchParams {
@XmlElement()
private Long msgCount;
@XmlElement()
private Date dateCreated;
public Long getMsgCount() {
...
Xml 文档为:
<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns="http://www.xxx/xsd/ems/batchreq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx/xsd/ems/batchreq ./ems_msg_batchreq.xsd" batchName="d" msgType="ems_sendemsg_batchreq" sourceSystem="1">
<batchParams>
<msgCount>1</msgCount>
<dateCreated>2014-12-03T12:00:00</dateCreated>
</batchParams>
<msgCollection xmlns="http://www.xxx/xsd/ems/req">
<msg msgType="ems_sendemsg_msg" sourceSystem="1" dataSourceSystem="1">
...
</msg>
</msgCollection>
</batch>
Element should not come with namespace - it does not have in in xml, nor in java class, yet it seems that when it is parsed, it gets namespace uri - and for bigger mystery, elements that have namespace do not seem to expect it.
您问题中的 XML 将 http://www.xxx/xsd/ems/batchreq
声明为默认命名空间。这意味着 XML 中未分配给不同命名空间的每个元素都将成为该命名空间的一部分。
<?xml version="1.0" encoding="UTF-8"?>
<batch xmlns="http://www.xxx/xsd/ems/batchreq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx/xsd/ems/batchreq ./ems_msg_batchreq.xsd" batchName="d" msgType="ems_sendemsg_batchreq" sourceSystem="1">
<batchParams>
<msgCount>1</msgCount>
<dateCreated>2014-12-03T12:00:00</dateCreated>
</batchParams>
<msgCollection xmlns="http://www.xxx/xsd/ems/req">
<msg msgType="ems_sendemsg_msg" sourceSystem="1" dataSourceSystem="1">
...
</msg>
</msgCollection>
</batch>
Adding namespace to @XmlElement helps, but then it seems that it would have to be defined for every single element - that is unacceptable solution.
您可以使用包级别 @XmlSchema
注释来映射默认名称空间限定。这在名为 package-info.java
的源文件中进行,其中仅包含如下所示的内容。您需要更改包名称以匹配您的域模型。
@XmlSchema(
namespace = "http://www.xxx/xsd/ems/batchreq",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Why is namespace not inherited by child elements?
JAXB 中的命名空间规范确实得到了继承,只是不是以您尝试过的方式。
- 包级别
@XmlSchema
注释提供了一种指定默认名称空间的方法,该名称空间应由对应于该包中 classes 和属性的所有元素继承。 - 类型级别
@XmlType
注释提供了一种方法来指定此 class 上所有属性继承的命名空间。这会覆盖@XmlSchema
注释中指定的命名空间信息。 - 最后,可以在
@XmlRootElement
、@XmlElement
注释(以及其他一些注释)上指定名称空间。此处指定的名称空间仅适用于该元素。
了解更多信息
我在我的博客上写了更多关于这个用例的文章: