Java 如何让JAXB schemagen允许一个@XmlRootElement类有多个元素名

Java 如何让JAXB schemagen允许一个@XmlRootElement类有多个元素名,java,jaxb,jaxb2,Java,Jaxb,Jaxb2,这是像这样的问题的相反方向。基本上,我希望运行schemagen并拥有两个相同类型的全局元素 <xs:element name="root1" type="tns:sameType"/> <xs:element name="root2" type="tns:sameType"/> 您可以在用@XmlRegistry注释的类上使用@xmlementdecl注释 package forum14845035; import javax.xml.bind.JAXBElemen

这是像这样的问题的相反方向。基本上,我希望运行schemagen并拥有两个相同类型的全局元素

<xs:element name="root1" type="tns:sameType"/>
<xs:element name="root2" type="tns:sameType"/>

您可以在用
@XmlRegistry
注释的类上使用
@xmlementdecl
注释

package forum14845035;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="root1")
    public JAXBElement<SameType> createRoot1(SameType sameType) {
        return new JAXBElement<SameType>(new QName("urn:example", "root1"), SameType.class, sameType);
    }

    @XmlElementDecl(name="root2")
    public JAXBElement<SameType> createRoot2(SameType sameType) {
        return new JAXBElement<SameType>(new QName("urn:example", "root2"), SameType.class, sameType);
    }

}
ObjectFactory

当一个类型具有多个对应的全局元素时,将使用
@xmlementdecl
注释。注释放在用
@XmlRegistry
注释的类上的
create
方法上。从XML模式生成模型时,此类始终称为
ObjectFactory

package forum14845035;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="root1")
    public JAXBElement<SameType> createRoot1(SameType sameType) {
        return new JAXBElement<SameType>(new QName("urn:example", "root1"), SameType.class, sameType);
    }

    @XmlElementDecl(name="root2")
    public JAXBElement<SameType> createRoot2(SameType sameType) {
        return new JAXBElement<SameType>(new QName("urn:example", "root2"), SameType.class, sameType);
    }

}
套餐信息

package forum14845035;

import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SameType.class, ObjectFactory.class);
        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceUri,
                    String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}
我们将利用包级别的
@XmlSchema
注释为我们的模型指定名称空间限定

@XmlSchema(namespace="urn:example", elementFormDefault=XmlNsForm.QUALIFIED)
package forum14845035;

import javax.xml.bind.annotation.*;
演示

package forum14845035;

import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SameType.class, ObjectFactory.class);
        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceUri,
                    String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}
输出


了解更多信息


太好了,谢谢。我一直在使用jaxb.index,甚至没有想到将ObjectFactory作为一种可能的途径。