在项目 jaxb unmarshall 中找不到具有默认根元素的描述符
Descriptor with default root element was not found in the project jaxb unmarshall
我的 xml 文件中的根元素如下:
<Document xmlns="urn:kad:ns:file:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
和XSD header如下:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="urn:kad:ns:file:1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
当我尝试解组文件时:
JAXBContext jc = JAXBContext.newInstance("path.to.package.of.schema.objects");
Unmarshaller u = jc.createUnmarshaller();
// read the file stream and unmarshall the XML
File f = new File(xmlFilePath);
Document document = (Document)u.unmarshal(f);
我收到以下异常:
Exception [EclipseLink-25008] (Eclipse Persistence Services -
2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.XMLMarshalException Exception
Description: A descriptor with default root element
{urn:kad:ns:file:1}Document was not found in the project
这是特定于在 XSD 中将 urn 作为目标名称空间的东西,还是我在解组 XML 文件的 JAVA 文件中遗漏了什么?
编辑
当我从 xml 文件 xmlns="urn:kad:ns:file:1"
中删除默认命名空间声明时,解组过程工作正常。但是没有验证发生,这意味着如果我从 XML 中删除一个必需的元素,该过程将在 JAVA 级别继续,并将此元素映射 object 视为 null。
我想知道这(根元素中的默认命名空间属性)对解组过程有什么影响?它会立即根据我指定的 URN 上的 XSD 验证 XML(我怀疑它在我的情况下找不到)吗?
当我对 XSD 进行解组时,如何验证 XML?
问题出在包内-info.java class。
使用 XMLSchema
注释的包,其默认名称空间设置为 urn:kad:ns:file:1
,与 XML(文档)的 Root 映射对象中使用的包不同.
因此,当 JAXB 尝试解组 XML 时,它无法在包命名空间中找到 (Document) 元素,因为首先没有定义命名空间。
Document.java
package path.to.pkg.jaxbobjects;
....
包-info.java
@javax.xml.bind.annotation.XmlSchema(namespace =
"urn:kad:ns:file:1",
elementFormDefault =
javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package path.to.pkg.jaxbobjects2;
删除默认命名空间时,将跳过此操作并允许解组文件。
我的 xml 文件中的根元素如下:
<Document xmlns="urn:kad:ns:file:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
和XSD header如下:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="urn:kad:ns:file:1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
当我尝试解组文件时:
JAXBContext jc = JAXBContext.newInstance("path.to.package.of.schema.objects");
Unmarshaller u = jc.createUnmarshaller();
// read the file stream and unmarshall the XML
File f = new File(xmlFilePath);
Document document = (Document)u.unmarshal(f);
我收到以下异常:
Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.XMLMarshalException Exception Description: A descriptor with default root element {urn:kad:ns:file:1}Document was not found in the project
这是特定于在 XSD 中将 urn 作为目标名称空间的东西,还是我在解组 XML 文件的 JAVA 文件中遗漏了什么?
编辑
当我从 xml 文件 xmlns="urn:kad:ns:file:1"
中删除默认命名空间声明时,解组过程工作正常。但是没有验证发生,这意味着如果我从 XML 中删除一个必需的元素,该过程将在 JAVA 级别继续,并将此元素映射 object 视为 null。
我想知道这(根元素中的默认命名空间属性)对解组过程有什么影响?它会立即根据我指定的 URN 上的 XSD 验证 XML(我怀疑它在我的情况下找不到)吗?
当我对 XSD 进行解组时,如何验证 XML?
问题出在包内-info.java class。
使用 XMLSchema
注释的包,其默认名称空间设置为 urn:kad:ns:file:1
,与 XML(文档)的 Root 映射对象中使用的包不同.
因此,当 JAXB 尝试解组 XML 时,它无法在包命名空间中找到 (Document) 元素,因为首先没有定义命名空间。
Document.java
package path.to.pkg.jaxbobjects;
....
包-info.java
@javax.xml.bind.annotation.XmlSchema(namespace =
"urn:kad:ns:file:1",
elementFormDefault =
javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package path.to.pkg.jaxbobjects2;
删除默认命名空间时,将跳过此操作并允许解组文件。