参数字符串的JAXBElement [英] parameter JAXBElement String
问题描述
我对JAXB简单的问题。下面是示例code:
I' have simple question about JAXB. Here is the sample code:
//setter that has input JAXBElement
b.setBIC(JAXBElement<String> value);
我怎么能初始化输入元素,使用字符串从其他物体?
How can I initialize the input element, that uses String from other object?
推荐答案
您可以直接创建的JAXBElement
的一个实例,或者如果你从XML模式使用中产生的Java模型对生成的的ObjectFactory
类中的方法,便于学习
You can create an instance of JAXBElement
directly or if you generated your Java model from an XML schema use a convience method on the generated ObjectFactory
class.
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
上面的演示code是基于以下XML架构生成的Java模型。究其原因,你收到了的JAXBElement&LT;弦乐&GT;
在首位属性是当你有一个元素都的nillable =真
和的minOccurs =0
。
The above demo code is based on a Java model generated from the following XML schema. The reason you get a JAXBElement<String>
property in the first place is when you have an element that is both nillable="true"
and 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
生成以下类包含像在你的问题的属性。
The following class was generated from schema.xsd
and contains properties like the one in your question.
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
下面是包含用于创建的的JAXBElement
的情况下,方便的方法生成的的ObjectFactory
类。
Below is the generated ObjectFactory
class that contains convenience methods for creating the instances of 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);
}
}
这篇关于参数字符串的JAXBElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!