1

pom.xml:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>
        <execution>
            <id>generate-config</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <schemaIncludes>
                    <include>config/main-config.xsd</include>
                    <include>config/status.xsd</include>
                </schemaIncludes>
                <bindingDirectory>src/main/resources/xjb/config</bindingDirectory>
                <bindingIncludes>
                    <bindingInclude>config-bindings.xjb</bindingInclude>
                </bindingIncludes>
                <generateDirectory>target/generated-sources/xjc/config</generateDirectory>
                <generatePackage>com.core.config</generatePackage>
                <packageLevelAnnotations>true</packageLevelAnnotations>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.hyperjaxb3</groupId>
            <artifactId>maven-hyperjaxb3-plugin</artifactId>
            <version>0.6.0</version>
        </dependency>
    </dependencies>
</plugin>

config-bindings.xjb:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
        version="2.1"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:annox="http://annox.dev.java.net"
        jaxb:extensionBindingPrefixes="annox">

    <jaxb:bindings >
        <jaxb:bindings schemaLocation="../../xsd/config/config.xsd" node="/xs:schema">
            <jaxb:bindings node="xs:complexType[@name='Configuration']">
                <annox:annotate>
                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="Conf"/>
                </annox:annotate>
            </jaxb:bindings>
        </jaxb:bindings>

        <jaxb:bindings schemaLocation="../../xsd/config/status.xsd" node="/xs:schema">
            <jaxb:bindings node="xs:complexType[@name='StatusInfo']">
                <annox:annotate>
                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="Status"/>
                </annox:annotate>
            </jaxb:bindings>
        </jaxb:bindings>

    </jaxb:bindings>

</jaxb:bindings>

Auto generated package-info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.company.com/configuration", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.core.config;

config.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:cfg="http://www.company.com/configuration"
           targetNamespace="http://www.company.com/configuration"
           elementFormDefault="qualified"
>

    <xs:element name="Conf" type="cfg:Configuration">
        <xs:annotation>
            <xs:documentation>
                Root element for the XML configuration.
            </xs:documentation>
        </xs:annotation>
    </xs:element>

    <xs:complexType name="Configuration">
        //My future Java Bean
    </xs:complexType>

</xs:schema>

status.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:cfg="http://www.company.com/configuration-status"
           targetNamespace="http://www.company.com/configuration-status"
           elementFormDefault="qualified"
>

    <xs:element name="Status" type="cfg:StatusInfo">
        <xs:annotation>
            <xs:documentation>
                Root element for the XML configuration.
            </xs:documentation>
        </xs:annotation>
    </xs:element>

    <xs:complexType name="StatusInfo">
        //My future Java Bean
    </xs:complexType>

</xs:schema>

I make marshaller.marshal(config, writer); and it generates

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Status xmlns="http://www.company.com/configuration-status" xmlns:ns2="http://www.company.com/configuration">
    <Test></Test>
</ns2:Status>

When I make unmarshaller.unmarshal(reader);

And got the exception:

Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 162; cvc-elt.1: Cannot find the declaration of element 'ns2:Status'.

So it just mixed namespaces and set incorrect prefix "ns2" for the XmlRootElement. Status element should point to the xmlns="http://www.company.com/configuration-status" not to the xmlns:ns2="http://www.company.com/configuration". I don't know why it happened. Also I tried to hardcode xml manually before unmarshal it and get the similar SAXParseException.

I've played with different examples how to manually track namespaces by extends NamespacePrefixMapper and prefixed by extends XMLFilterImpl or/and extends DefaultHandler but unfortunately these don't help me.

Nick
  • 109
  • 4
  • 11
  • 2
    You created all classes under `com.core.config` so you cannot have two different `package-info.java` indeed has been replaced with the last one. Try to remove `com.core.config`. – Xstian Aug 10 '17 at 14:47
  • Yeah, thank you, U save me one more day:).I've done it by this example https://stackoverflow.com/a/2857613/3343155 and that solved my problem. – Nick Aug 11 '17 at 07:49

0 Answers0