首页 > 解决方案 > SAXParseException 列出可选值

问题描述

我在 Windows 10 上运行的 Java 1.8.0_191 程序中收到以下 UnmarshalException,该程序使用使用 jaxb2 Maven 插件版本 2.5.0 生成的类来处理传入的 XML SOAP 请求:

JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 622; cvc-complex-type.2.4.a : Contenu non valide trouvé à partir del'élément 'MotifCommande'. L'une des valeurs '{AliasCde,RefExterneCde, DateDepot}' est attendue.]

这是异常消息的英文快速翻译:

发现以 MotifCommand 项目开头的无效内容。需要项目“{AliasCd, RefExterneCde, DateDepot}”之一。

奇怪的是,项目列表中提到了两个可选字段(AliasCD 和 RefExterneCde)和一个必填字段(DateDepot)。为什么不只是必填字段?

此外,为什么在 XSD 中,尽管 MotifCommande 标记出现(强制)DateDepot 标记之后,但消息指出内容无效?

XSD 和 SOAP 请求以 zip 格式提供,位于此网站:https ://github.com/highsource/maven-jaxb2-plugin/issues/185

非常感谢您的帮助。

标签: javasax

解决方案


错误消息列出了文档中该位置允许的所有元素。这还包括可选元素。如果出现其他任何内容,则为错误。

XML 模式中的相关部分是这样的:

<xsd:sequence>
  <xsd:element name="SystemeRefCde" type="xsd:string" minOccurs="1"/>
  <xsd:element name="AliasCde" minOccurs="0" maxOccurs="unbounded">
  <xsd:element name="RefExterneCde" minOccurs="0" maxOccurs="unbounded">
  <xsd:element name="DateDepot" type="xsd:string" minOccurs="1"/>
  <!-- ... -->
  <xsd:element name="MotifCommande" type="xsd:string" minOccurs="0"/>

您正在使用序列。因此,项目的顺序是相关的。这意味着,SystemeRefCde仅在 optinoal 元素之后AliasCdeRefExterneCde之后,DateDepot可能会出现强制元素。每个其他元素都会导致架构冲突。

在您的请求中,您有:

<RefCommande>32R69010200004101415</RefCommande>
<SystemeRefCde>32R</SystemeRefCde>

<MotifCommande>ORDST</MotifCommande>

SystemeRefCde序列中的下一个元素之后是MotifCommande。XSD 不允许这样做。您至少缺少DateDepot介于两者之间的强制性元素。


推荐阅读