简体   繁体   中英

JAXB Create New Element Using binding XML

I'm using XJC to create JAXB annotated Java classes based on a schema file. My issue is that the schema file provided by a third party is missing a field on an object that actually is present in the XML message itself. Is there a way using an XML binding file to create the field? I've been attempting variations on the bindings below to no avail. In this case I'm attempting to create a field called "Bar" on the "Foo" object. It should be of type boolean, but I'm still trying to figure out where to specify that.

<jaxb:bindings
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings localScoping="toplevel"/>
    <jaxb:bindings schemaLocation="mySchemaFile.xsd">
        <jaxb:bindings node="//xs:element[@name='Foo']">
            <jaxb:property name="Bar" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

The XML schema defines what makes a valid XML document. To the best of my knowledge, JAXB only intends to provide support for parsing/writing valid XML documents. While there are certainly some errors in XML documents that JAXB will read/write if you don't turn on validation, I would say that this is a side-effect of the implementation rather than a design requirement.

I don't know of a way to get JAXB to create the extra property that you are looking for. And certainly not as part of the binding file.

It is possible to get the JAXB-generated classes to extend a class of your choosing, which could have the extra property. Perhaps if you put the right annotations on that property you could get it to be read/written as XML.

I don't know what constraints you have, but given that you want to accept invalid XML documents you might consider grabbing the third-party XSD and maintain your own (extended) version of that. Then you can not deal with trying to work around this issue and you could actually turn on validation.

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