问题描述
我正在使用Java和JAXB进行XML处理。
I am using Java and JAXB for XML processing.
我有以下课程:
public class Characteristic {
private String characteristic;
private String value;
@XmlAttribute
public String getCharacteristic() {
return characteristic;
}
public void setCharacteristic(String characteristic) {
this.characteristic = characteristic;
}
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static void main(String[] args) {
Characteristic c = new Characteristic();
c.setCharacteristic("store_capacity");
c.setValue(40);
Characteristic c2 = new Characteristic();
c2.setCharacteristic("number_of_doors");
c2.setValue(4);
}
这是我得到的结果:
<characteristics characteristic="store_capacity">40</characteristics>
<characteristics characteristic="number_of_doors">4</characteristics>
我想得到以下结果:
<store_capacity>40</store_capacity>
<number_of_doors>4</number_of_doors>
我如何实现这一目标?
推荐答案
您可以结合使用和生成动态元素名称。
You can use a combination of @XmlElementRef and JAXBElement to produce dynamic element names.
这个想法是:
- 使
特征
JAXBElement
的子类并覆盖getName()
根据特征
属性返回名称的方法。 - 注释
特征
使用@XmlElementRef
。 - 提供
@XmlRegistry
(ObjectFactory
),@XmlElementDecl(name =characteristic)
。
- Make
Characteristic
a subclass ofJAXBElement
and override thegetName()
method to return the name based on thecharacteristic
property. - Annotate
characteristics
with@XmlElementRef
. - Provide the
@XmlRegistry
(ObjectFactory
) with an@XmlElementDecl(name = "characteristic")
.
以下是。
测试本身(没什么特别的):
The test itself (nothing special):
@Test
public void marshallsDynamicElementName() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
final Characteristics characteristics = new Characteristics();
final Characteristic characteristic = new Characteristic(
"store_capacity", "40");
characteristics.getCharacteristics().add(characteristic);
context.createMarshaller().marshal(characteristics, System.out);
}
产生:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<characteristics><store_capacity>40</store_capacity></characteristics>
让我们从特征开始
根元素类。它有一个特征
属性,用 @XmlElementRef
注释。这意味着内容应该是 JAXBElement
s或 @XmlRootElement
-annotated class instances。
Let's start with the characteristics
root element class. It has a characteristics
property which is annotated with @XmlElementRef
. This means that the contents should be either JAXBElement
s or @XmlRootElement
-annotated class instances.
@XmlRootElement(name = "characteristics")
public class Characteristics {
private final List<Characteristic> characteristics = new LinkedList<Characteristic>();
@XmlElementRef(name = "characteristic")
public List<Characteristic> getCharacteristics() {
return characteristics;
}
}
为了让你工作还需要一个 ObjectFactory
或者用 @XmlRegistry
注释的东西,有一个相应的 @XmlElementDecl
:
In order for this to work you also need an ObjectFactory
or something annotated with @XmlRegistry
having a corresponding @XmlElementDecl
:
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name = "characteristic")
public JAXBElement<String> createCharacteristic(String value) {
return new Characteristic(value);
}
}
回想一下,特性
属性必须包含 @XmlRootElement
-annotated类实例或 JAXBElement
s。 @XmlRootElement
不合适,因为它是静态的。但 JAXBElement
是动态的。您可以继承 JAXBElement
并覆盖 getName()
方法:
Recall, the characteristics
property must contain either @XmlRootElement
-annotated class instances or JAXBElement
s. @XmlRootElement
is not suitable since it's static. But JAXBElement
is dynamic. You can subclass JAXBElement
and override the getName()
method:
public class Characteristic extends JAXBElement<String> {
private static final long serialVersionUID = 1L;
public static final QName NAME = new QName("characteristic");
public Characteristic(String value) {
super(NAME, String.class, value);
}
public Characteristic(String characteristic, String value) {
super(NAME, String.class, value);
this.characteristic = characteristic;
}
@Override
public QName getName() {
final String characteristic = getCharacteristic();
if (characteristic != null) {
return new QName(characteristic);
}
return super.getName();
}
private String characteristic;
@XmlTransient
public String getCharacteristic() {
return characteristic;
}
public void setCharacteristic(String characteristic) {
this.characteristic = characteristic;
}
}
在这种情况下,我覆盖了 getName()
动态确定元素名称的方法。如果设置了特性
属性,则其值将用作名称,否则方法将选择默认特征
元素。
In this case I've overridden the getName()
method to dynamically determine the element name. If characteristic
property is set, its value will be used as the name, otherwise the method opts to the default characteristic
element.
测试代码是。
这篇关于如何使用JAXB使类字段成为标记名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!