我正在通过 Validator 类验证我的 jaxb 对象。下面是我用来验证 jaxb 对象的代码。但是在验证它时我收到了这个错误。
jc = JAXBContext.newInstance(obj.getClass());
source = new JAXBSource(jc, obj);
Schema schema = schemaInjector.getSchema();
Validator validator = schema.newValidator();
validator.validate(source);
错误(SAXParseException):cvc-complex-type.2.4.a:发现以元素“ProcessDesc”开头的无效内容。应为 ProcessName 之一
我不明白我在 xsd 中做错了什么导致了这个错误。我的 xsd 文件中定义的元素低于我收到的错误。
<xs:schema xmlns:cc="http://www.ms.com/cm.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ms.com/cm.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Process">
<xs:sequence>
<xs:element name="ProcessId" type="xs:int" />
<xs:element name="ProcessName" type="xs:string" />
<xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
请帮我解决这个问题。谢谢。
最佳答案
XML 模式代码
<xs:complexType name="Process">
<xs:sequence>
<xs:element name="ProcessId" type="xs:int" />
<xs:element name="ProcessName" type="xs:string" />
<xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
描述一些应该看起来像的 XML
<proc> <!-- of type Process -->
<ProcessId>123</ProcessId>
<ProcessName>procA</ProcessName>
<ProcessDesc>A funny process</ProcessDesc> <!-- this could be omitted -->
<proc>
但是你的 XML 数据看起来像
<proc> <!-- of type Process -->
<ProcessId>123</ProcessId>
<ProcessDesc>A funny process</ProcessDesc>
<!-- ... don't know what follows -->
如果您不关心 Id、Name、Desc 的顺序,则必须更改 XML 架构。否则,您将不得不修复 XML(这更容易)。
如果您认为“元素的任意顺序”是个好主意,请使用:
<xs:complexType name="Process">
<xs:all>
<xs:element name="ProcessId" type="xs:int" />
<xs:element name="ProcessName" type="xs:string" />
<xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
</xs:all>
</xs:complexType>
关于xml - cvc-complex-type.2.4.a : invalid content was found starting with element 'ProcessDesc' . ProcessName 预期之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174301/