use*_*213 14 xml schema xsd jaxb
我通过Validator类验证我的jaxb对象.下面是我用来验证jaxb对象的代码.但在验证它时,我收到了这个错误.
jc = JAXBContext.newInstance(obj.getClass());
source = new JAXBSource(jc, obj);
Schema schema = schemaInjector.getSchema();
Validator validator = schema.newValidator();
validator.validate(source);
Run Code Online (Sandbox Code Playgroud)
ERROR(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>
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.谢谢.
lau*_*une 22
XML Sehema代码
<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>
Run Code Online (Sandbox Code Playgroud)
描述了一些应该看起来像的XML
<proc> <!-- of type Process -->
<ProcessId>123</ProcessId>
<ProcessName>procA</ProcessName>
<ProcessDesc>A funny process</ProcessDesc> <!-- this could be omitted -->
<proc>
Run Code Online (Sandbox Code Playgroud)
但是您的XML数据看起来像
<proc> <!-- of type Process -->
<ProcessId>123</ProcessId>
<ProcessDesc>A funny process</ProcessDesc>
<!-- ... don't know what follows -->
Run Code Online (Sandbox Code Playgroud)
如果您不关心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>
Run Code Online (Sandbox Code Playgroud)
这些错误是由于以下原因之一
元素名称输入错误。
试图使用架构中未描述的元素。
元素顺序错误。
在根标记或父元素中声明的命名空间定义与该元素中使用的前缀(或没有前缀)不匹配。
Java对象在xsd中需要一个空字段