How to get JAXB output to have namespace included

2019-03-02 12:07发布

问题:

God knows I searched the forum for an answer, but didn't see any. This is the simplified XML my JAXB code reads. There are 2 namespaces involved. xyz and abc. These two are defined in two different schema files. And xjc generates two different packages for them. The following file is nicely read into those classes and can even write it.

<xyz:xyz xsi:schemaLocation="urn:xyz xyz.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xyz="urn:xyz">
    <session>
        <App xsi:schemaLocation="urn:abc abc.xsd" xmlns="urn:abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <AppItem att1="1234"/>
        </App>
    </session>
</xyz:xyz>

This is how it writes it.

<ns3:xyz xmlns:ns2="urn:abc" xmlns:ns3="urn:xyz">
    <session>
        <ns2:App>
            <ns2:AppItem att1="1234"/>
        </ns2:App>
    </session>
</ns3:xyz>

Now i know about NamespacePrefixMapper and I can change ns2 and ns3 to the values I want. And I want this. Basically I want to main the original form of the XML. The App element should have all its information contained in itself and not create a prefix.

<xyz:xyz xmlns:xyz="urn:xyz">
    <session>
        <App xsi:schemaLocation="urn:abc abc.xsd" xmlns="urn:abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <AppItem att1="1234"/>
        </App>
    </session>
</xyz:xyz>

Does anyone have any clue as to how to achieve this? Seems like some setting in AppType.java should tell the writer to not update root element with prefix.