gpt4 book ai didi

java - 在 JAXB 中编码时如何避免根元素注释?

转载 作者:行者123 更新时间:2023-12-02 04:44:12 26 4
gpt4 key购买 nike

我想将对象数据存储为 XML。下面的代码 spinets 将显示模型类的示例。

Class Model
{
@XmlElement
private int id;
@XmlElement
Private string name;
}

我将有多个模型对象,它们将存储在如下列表中

@XmlRootElement
Class ModelWrapper
{
@XmlElement
@XmlJavaTypeAdapter(value = ListAdapter.class, type = List.class)
List<model> list;
public setlist(List<model>list)
{
//setting list
}
public List<model> getlist()
{
return list
}
}

现在,如果我使用 JAXB 对其进行编码,将会生成如下内容:

<Modelwrapper>
<List>
......
......
</List>
</Modelwrapper>

我想避免根之一可能是列表或模型包装器。有什么办法可以做到吗?

最佳答案

这样你的xml就会是

<ModelWrapper>
<model>
......
</model>
<model>
......
</model>
</ModelWrapper>

ModelWrapper.java

@XmlRootElement
public class ModelWrapper {

@XmlElement(name = "model")
protected List<Model> list;
<小时/>

测试

ModelWrapper.java

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ModelWrapper", propOrder = {
"list"
})
public class ModelWrapper {

@XmlElement(name = "model")
private List<Model> list;

public List<Model> getList() {
return list;
}

public void setList(List<Model> list) {
this.list = list;
}

}

模型.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Model", propOrder = {
"id","name"
})
public class Model {
@XmlElement
private int id;
@XmlElement
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

Main.java

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {

public static void main(String[] args) throws JAXBException {

JAXBContext jc = JAXBContext.newInstance(ModelWrapper.class);

ModelWrapper mw = new ModelWrapper();

List<Model> list = new ArrayList<Model>();
Model m = new Model();
m.setId(1);
m.setName("model1");
list.add(m);
m = new Model();
m.setId(1);
m.setName("model2");
list.add(m);

mw.setList(list);

Marshaller mar = jc.createMarshaller();

mar.marshal(mw, System.out);


}

}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<modelWrapper>
<model>
<id>1</id>
<name>model1</name>
</model>
<model>
<id>1</id>
<name>model2</name>
</model>
</modelWrapper>

关于java - 在 JAXB 中编码时如何避免根元素注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com