- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个关于 JAXB 的简单问题。这是示例代码:
//setter that has input JAXBElement
b.setBIC(JAXBElement<String> value);
如何初始化使用来自其他对象的字符串的输入元素?
最佳答案
您可以直接创建 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
类,其中包含用于创建 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);
}
}
关于java - 参数 JAXBElement 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14489306/
以下代码有效: private List custom; @XmlAnyElement public List getCustom() { if (custom == null) {
我有一个使用 Spring (v4.0.5) 的 http 服务。它的 http 端点是使用 Spring Web MVC 配置的。响应是根据模式生成的 JAXB2 注释类。响应被包装在 JAXBEl
我有一个 xsd,其中包含替换组。当我尝试使用 cxf wsdl2java JAXB 生成 Java 类时,生成 List> 而不是 List 。如何避免 JAXBElement?我尝试使用 bind
我想从 XSD 文件生成带有 JAXB 的 Java 类。 问题是,我总是得到一些像这样的类(删除了命名空间): public static class Action { @X
我在用: GSON 2.3.1 apache-cxf-3.1.1 JBoss EAP 6.4 我正在使用 wsdl2java 从 wsdl 创建 webservice 客户端。 我用以下方法生成类:
我有一个完全可用且经过测试的 REST API。我正在使用 enunciate 创建文档。 请求和响应对象是使用 jaxb 从 xsd 文件生成的。在文档中,可以识别响应对象,但我的请求正文类型是(自
我想实现一个返回 JAXBElement 的方法,下面是代码 @XmlRootElement(name = "history") @XmlAccessorType(XmlAccessType.FIEL
我有多个根元素,因此,我需要写 JAXBElement jaxbBookType = objectFactory.createBookType (bookType); JAXBElement jaxb
考虑 JAXB 生成的以下代码: public class CnpOnlineResponse { protected JAXBElement transactionResponse;
我有以下代码, JAXBContext jc = JAXBContext.newInstance(TestResults.class); Marshaller m = jc.createMarshal
当我尝试从 wsdl 文档生成客户端时,我得到的客户端似乎有很多 JAXBElement 属性,例如 protected List> nameOrLinkingNameOrFamilyName; 我使
我正在使用 NetBeans 和 Glassfish 从现有 WSDL 构建 Web 服务。NetBeans 已从给定的 WSDL 创建了所需的类。WSDL 定义了一些基本数据类型(例如 BaseTy
我定义了一个 Book 类,我想创建一个 JAXBElement 对象,该对象将包含与来自 String 对象的 XML 对应的信息。 例如,我可以有这样的东西: String code = "Har
如何编码 JAXBElement 列表? 例如,我有一个无法注释的 POJO: public class APojo { private String aString; public APojo()
我有一个方法 public Response xyz ( JAXBElement request ) { ...... } 两个不同的地方使用不同的 JAXBElement 调用它。一与 xyz(JA
您好,我想将 JAXBelement 值存储到 String 变量,请告诉我该怎么做。我有以下方法。 public JAXBElement getSessionId() { return s
我已经从我的 XML .xsd 生成了类,并且正在尝试设置一个字段 EndpointID在类(class)内MeterSessionInputRF .我遇到的问题是 setEndpointID方法只接
我正在学习 JAX-WS,并且还介绍了 JAXB。我可以用它来将 java(xml) 编码(un)编码为 xml(java)。但是这行代码的目的是什么,然后我使用 Apache CXF 生成了 WS
我有一个关于 JAXB 的简单问题。这是示例代码: //setter that has input JAXBElement b.setBIC(JAXBElement value); 如何初
我有一个名为 MYClass 的类,其代码如下所示 package com.rest; public class MyClass { private String var; publi
我是一名优秀的程序员,十分优秀!