有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JAXB:名称空间前缀不正确

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>

配置绑定。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>

自动生成包信息。java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.company.com/configuration", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.core.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>

状态。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>

我做 marshaller.marshal(config, writer);并生成

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

当我做unmarshaller.unmarshal(reader);

我得到了一个例外:

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

因此它只是混合了名称空间,并为XmlRootElement设置了不正确的前缀“ns2”Status元素应指向xmlns=”http://www.company.com/configuration-status“不适用于xmlns:ns2=”http://www.company.com/configuration“。 我不知道为什么会这样。我还尝试在解组xml之前手动硬编码xml,并获得类似的SAXParseException

我用过不同的例子来演示如何通过extends NamespacePrefixMapper和前缀extends XMLFilterImpl或/和extends DefaultHandler来手动跟踪名称空间,但不幸的是,这些对我没有帮助


共 (0) 个答案