参数JAXBElement String

时间:2013-01-23 21:01:33

标签: java jaxb annotations

我对JAXB有一个简单的问题。以下是示例代码:

   //setter that has input JAXBElement
   b.setBIC(JAXBElement<String> value);

如何初始化使用其他对象的String的input元素?

2 个答案:

答案 0 :(得分:30)

您可以直接创建JAXBElement的实例,或者如果您从XML架构生成Java模型,则在生成的ObjectFactory类上使用convience方法。

package org.example.schema;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("org.example.schema");

        Root root = new Root();

        QName fooQName = new QName("http://www.example.org/schema", "foo");
        JAXBElement<String> fooValue = new JAXBElement<String>(fooQName, String.class, "FOO");
        root.setFoo(fooValue);

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<String> barValue = objectFactory.createRootBar("BAR");
        root.setBar(barValue);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

<强> schema.xsd

上面的演示代码基于从以下XML模式生成的Java模型。您首先获得JAXBElement<String>属性的原因是您的元素同时为nillable="true"minOccurs="0"

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">
    <element name="root">
        <complexType>
            <sequence>
                <element name="foo" type="string" minOccurs="0" nillable="true"/>
                <element name="bar" type="string" minOccurs="0" nillable="true"/>
            </sequence>
        </complexType>
    </element>
</schema>

<强>根

以下类是从schema.xsd生成的,其中包含您问题中的属性。

package org.example.schema;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"foo","bar"})
@XmlRootElement(name = "root")
public class Root {

    @XmlElementRef(name = "foo", namespace = "http://www.example.org/schema", type = JAXBElement.class)
    protected JAXBElement<String> foo;
    @XmlElementRef(name = "bar", namespace = "http://www.example.org/schema", type = JAXBElement.class)
    protected JAXBElement<String> bar;

    public JAXBElement<String> getFoo() {
        return foo;
    }

    public void setFoo(JAXBElement<String> value) {
        this.foo = value;
    }

    public JAXBElement<String> getBar() {
        return bar;
    }

    public void setBar(JAXBElement<String> value) {
        this.bar = value;
    }

}

<强>的ObjectFactory

以下是生成的ObjectFactory类,其中包含用于创建JAXBElement实例的便捷方法。

package org.example.schema;

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

@XmlRegistry
public class ObjectFactory {

    private final static QName _RootFoo_QNAME = new QName("http://www.example.org/schema", "foo");
    private final static QName _RootBar_QNAME = new QName("http://www.example.org/schema", "bar");

    public Root createRoot() {
        return new Root();
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "foo", scope = Root.class)
    public JAXBElement<String> createRootFoo(String value) {
        return new JAXBElement<String>(_RootFoo_QNAME, String.class, Root.class, value);
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "bar", scope = Root.class)
    public JAXBElement<String> createRootBar(String value) {
        return new JAXBElement<String>(_RootBar_QNAME, String.class, Root.class, value);
    }

}

答案 1 :(得分:0)

我们可以在下面创建JAXBElement对象: 对于可能有这个问题的其他人,对于b.setBIC给出的例子(JAXBElement值); 让我们假设您的类是ClassB,对象是b。

b.setBIC(new JAXBElement(new QName(ClassB.class.getSimpleName()), ClassB.class, "StringToBeInitialized"));