Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类中使用XSD选择字段进行编组_Java_Xml_Jaxb_Xsd_Marshalling - Fatal编程技术网

Java 在类中使用XSD选择字段进行编组

Java 在类中使用XSD选择字段进行编组,java,xml,jaxb,xsd,marshalling,Java,Xml,Jaxb,Xsd,Marshalling,我使用Java类动态生成WSDL模式。我将此作为我的一个字段: @XmlElements({ @XmlElement(name = "A", type = String.class), @XmlElement(name = "B", type = Integer.class), @XmlElement(name = "C", type = String.class), @XmlElement(name = "D", type = String.class) })

我使用Java类动态生成WSDL模式。我将此作为我的一个字段:

@XmlElements({
    @XmlElement(name = "A", type = String.class),
    @XmlElement(name = "B", type = Integer.class),
    @XmlElement(name = "C", type = String.class),
    @XmlElement(name = "D", type = String.class)
})
protected Object aOrBOrCOrD;
在编组过程中,当设置了单选属性aOrBOrCOrD时,XML中将设置哪个标记名(A、B、C或D)

因为只有一个字段包含数据。字符串也可以表示3个选项元素中的任意1个。如何避开这个问题


我是否可以将单个字段拆分为4,并在以某种方式生成WSDL时仍保留choice属性?

您可以执行以下操作:

Java模型 Foo

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

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

        StringReader xml = new StringReader("<foo><C>Hello World</C></foo>");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        JAXBElement<?> aOrBOrCOrD = foo.aOrBOrCOrD;
        System.out.println(aOrBOrCOrD.getName().getLocalPart());
        System.out.println(aOrBOrCOrD.getDeclaredType());
        System.out.println(aOrBOrCOrD.getValue());
    }

}
您可以使用
@xmlementrefs
类型的属性,而不是
@xmlementrefs
类型的属性。
JAXBElement
允许您保留元素名称

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRefs({
        @XmlElementRef(name = "A", type = JAXBElement.class),
        @XmlElementRef(name = "B", type = JAXBElement.class),
        @XmlElementRef(name = "C", type = JAXBElement.class),
        @XmlElementRef(name = "D", type = JAXBElement.class)
    })
    protected JAXBElement<?> aOrBOrCOrD;

}
演示代码 演示

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

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

        StringReader xml = new StringReader("<foo><C>Hello World</C></foo>");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        JAXBElement<?> aOrBOrCOrD = foo.aOrBOrCOrD;
        System.out.println(aOrBOrCOrD.getName().getLocalPart());
        System.out.println(aOrBOrCOrD.getDeclaredType());
        System.out.println(aOrBOrCOrD.getValue());
    }

}

谢谢你的回答。一个后续问题;如果我想为其中一个选项引入一个适配器,我该怎么做呢。我想将其中一个选项值转换为Base64或从Base64转换为Base64。
C
class java.lang.String
Hello World