简体   繁体   中英

JAXB ignores extended element while parsing

I am working on a piece of code that repurposes orm xmls by adding extra elements to the standard ones. I would like to use JAXB to parse the extended configuration files and JAXB parses my example xmls fine but skips the added element.

Here is some more detail. The extending xsd looks like this:

<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>

and the above is also the xsd I used for generation of Java classes. As for my test xml this is the content:

<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>

and the code that is use for testing is:

@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());
}

The second assert in the above test fails, meaning ext:entity tag is totally ignored. What am I missing here?

XML中的根元素是XSD中不存在的entity-mappings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM