我正在编写一段代码,通过向标准元素添加额外元素来重新定位orm xmls。我想使用JAXB来解析扩展配置文件,JAXB解析我的示例xmls,但跳过添加的元素。
以下是一些更详细的信息。扩展的xsd看起来像这样:
<xsd:schema targetNamespace="http://somewhere/xml/ns/orm_extension"
xmlns="http://somewhere/xml/ns/orm_extension"
xmlns:orm="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import schemaLocation="orm_2_1.xsd" namespace="http://xmlns.jcp.org/xml/ns/persistence/orm" />
<xsd:element name="entity">
<xsd:complexType>
<xsd:complexContent xml:base="orm:entity">
<xsd:extension base="orm:entity">
<xsd:sequence>
<xsd:element name="some-context" type="someContext" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="someContext">
........
</xsd:complexType>
</xsd:schema>
以上也是我用于生成Java类的xsd。至于我的测试xml,这是内容:
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" version="2.1"
xmlns:ext="http://somewhere/xml/ns/orm_extension"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://somewhere/xml/ns/orm_extension http://somewhere/xml/ns/orm_extension">
<ext:entity>
</ext:entity>
</entity-mappings>
并且用于测试的代码是:
@Test
public void testRead() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(EntityMappings.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
EntityMappings em = (EntityMappings) unmarshaller.unmarshal(
ClassLoader.getSystemResourceAsStream("schema/test.xml"));
assertNotNull(em);
assertEquals(1, em.getEntities().size());
}
上述测试中的第二个断言失败,意味着ext:entity标签被完全忽略。我在这里缺少什么?
答案 0 :(得分:0)
XML中的根元素是entity-mappings
,在XSD中不存在。