2013-01-23 122 views
12

我有关于JAXB的简单问题。下面是示例代码:参数JAXBElement字符串

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

我怎么能初始化input元素,使用字符串从其他对象?

+0

的[如何在Web服务使用的JAXBElement ?](http://stackoverflow.com/questions/1393920/how-to-use-jaxbelementstring-in-web-service) – Vadzim

回答

27

您可以直接创建JAXBElement的实例,或者如果您从XML模式生成Java模型,请在生成的ObjectFactory类上使用易理解方法。

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); 
    } 

} 
+0

可能重复号,我正在使用具有输入参数JAXBElement的Web服务。 QName究竟是什么? –

+0

@ extra90 - 我的更新是否回答您的评论? –

+0

是的,现在我明白QName是什么。但是我有点困惑,我有Web Service的客户端。并且服务具有输入参数JAXBElement的方法。我应该从Web服务生成JAXB类吗? –

0

我们可以做下面创建的JAXBElement对象: 只是为别人谁可能有这个问题,对于给定的b.setBIC(JAXBElement的值)的例子; 让我们假设你的班级是ClassB,而对象是b。

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