- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JAXB 加载一系列 XML 文件,为每个类创建多个对象列表。
图 block 类
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement//(name="Tiles")
@XmlAccessorType(XmlAccessType.FIELD)
public class Tile {
private String name;
public String getName(){
return name;
}
public void setName(String arg){
this.name = arg;
}
private int id;
public int getId() {
return id;
}
public void setId(int arg){
this.id = arg;
}
private String imageName;
public String getImageName(){
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
private int drawType;
public int getDrawType(){
return drawType;
}
public void setDrawType(int drawType) {
this.drawType = drawType;
}
private int trueType;
public int getTrueType() {
return trueType;
}
public void setTrueType(int trueType) {
this.trueType = trueType;
}
private int count;
public int getCount(){
return count;
}
public void setCount(int count) {
this.count = count;
}
private List<Integer> side;
public Integer getSide(int arg){
return side.get(arg);
}
public void setSide(List<Integer> side) {
this.side = side;
}
private int feature;
public int getFeature (){
return feature;
}
public void setFeature(int feature) {
this.feature = feature;
}
}
因此,当我真正尝试解码 XML 时,我认为首先编码该类的测试实例,以确认生成的 XML 文件的格式是个好主意。
JAXBContext jc = JAXBContext.newInstance(Tile.class,JAXB2_Lists.class,Feature.class );
JAXB2_Lists<Tile> exportTest = new JAXB2_Lists<>();
Marshaller marshaller = jc.createMarshaller();
Tile testTile = new Tile();
testTile.setCount(1);
testTile.setDrawType(1);
testTile.setFeature(1);
testTile.setId(1);
testTile.setImageName("TestImage.png");
testTile.setName("Test Name");
testTile.setTrueType(1);
List<Integer> sides = new ArrayList<>();
testTile.setSide(sides);
exportTest.getValues().add(testTile);
marshaller.marshal(exportTest, new File("TilesExport.xml"));
生成的 XML 如下所示。
<jaxb2Lists>
<tile>
<name>Test Name</name>
<id>1</id>
<imageName>TestImage.png</imageName>
<drawType>1</drawType>
<trueType>1</trueType>
<count>1</count>
<feature>1</feature>
</tile>
</jaxb2Lists>
下面的 URL 详细介绍了 JAXB_List 类,这是我的另一个问题。 (显然没有人能回答这个问题。 https://stackoverflow.com/questions/38472060/dynamic-xmlrootelement-name
因此生成的 XML 具有除 Sides 成员变量之外的所有值。
我哪里出错了?
最佳答案
您的 sides
列表需要一个公共(public) getter,并且 setter 需要命名为 sides
。像这样的东西:
public List<Integer> getSides() {
return side;
}
public void setSides(List<Integer> sides) {
this.side = sides;
}
应该这样做。为了保持一致性,也可能值得将您的 side
字段重命名为 sides
。
关于java - JAXB 编码/解码具有 List 变量成员的类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516780/
我正在考虑使用 JAXB 进行 XML 解析,但到目前为止我遇到了一些问题,这让我相信它可能不够灵活,无法满足我的需求。 我将解析第三方提供的 XML 以符合我将发布的 XSD。所以我想足够灵活地处理
我正在考虑使用 JAXB 进行 XML 解析,但到目前为止我遇到了一些问题,这让我相信它可能不够灵活,无法满足我的需求。 我将解析第三方提供的 XML 以符合我将发布的 XSD。所以我想足够灵活地处理
我有一个 xsd,用于使用 JAXB 在 Java 中生成对象模型,并且我希望将它生成的列表重命名为 xyzList 而不是 xyz。有没有办法做到这一点,而不必在绑定(bind)文件中为每个列表添加
我们正在使用 JAXB 构建许多开发人员应用程序,并不断遇到问题,这些问题都归结为 JAXB 对象的生产者和消费者之间的“版本”不匹配。 过程并没有减轻痛苦,所以我正在考虑一些类似于 JAXB 的 C
我们有一个在一个属性上带有 JAXB 注释的类。然后我们有几个子类来注释其余的重要数据。然而,我们有一个子类,我们想忽略父类注释,这样它就不会被编码。这是一些示例代码。 父类: @XmlType(na
我一直在使用 Oracle JDK 1.7 附带的默认 Sun JAXB 实现。 不幸的是,我有一些非常复杂的 XSD 模式可以使用,而且我发现了 XSD 到 Java 引擎中的一个错误(在 this
我有这个 Maven“任务”来使用 JAXB 从 XSD 文件生成 Java 类。 com.sun.tools.xjc.maven2
我想要这样的 XML: Foo 我可以通过一个看起来像这样的 JAXB 类成功地做到这一点: @XmlRootElement(name="simple") class Simple { @Xm
我有一个 POJO,我想将其编码并作为 xml 发送,但也想解编从同一个 Web 服务返回的数据。但是,此 POJO 中有某些字段,我不想发送,但可能在服务器返回的 xml 中。 这里的问题是,如果我
嘿伙计们,希望这里是一个简单的好东西。 我正在使用 JAXB 从架构生成类,并且我希望能够使用访问者模式来处理它们。 为此,我认为我需要每个 JAXB 生成的类来实现我定义的接口(interface)
我有一个 POJO,我想将其编码并作为 xml 发送,但也想解编从同一个 Web 服务返回的数据。但是,此 POJO 中有某些字段,我不想发送,但可能在服务器返回的 xml 中。 这里的问题是,如果我
这是我的转换代码。当我们处理大数据时,这需要很长时间......调用该方法几乎一百万次......我们可以清楚地看到它持有线程一段时间。 请建议我一些提高性能的方法! public class Gen
我有一个可以成功编码的 jaxb 对象,它有一个列表对象,然后我创建一个如下所示的新对象 public class Sub extends SuperJAXBClass{ @Override publ
我已经将jaxb库更新为2.0版。 我正在使用以下jars jaxb-api-2.0.jar和jaxb-imp-2.0.jar。 现在的问题是,它没有使用@XmlRootElement(name =“
我在使用 JAXB 时面临以下问题:看起来 JAXB 正在分析从最深的子类到父类的属性,并且子属性具有优先级。我想以某种方式改变这种行为。特别是: 子类: package test.sub; impo
我有以下 XML 消息,我需要反序列化它 我该如何处理? 最佳答案 @XmlElements注释用于映射选择结构。 http://b
我们想用 JAX-RS 和 JAXB 实现一个 RESTful-Web 服务。我们有一个使用 xml 的 PUT 方法,如下所示: -1 test test@gmx.de
我有以下代码 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class SamplePojo { private S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 2 年前。
所以我有一个 Jersey 的 REST 服务设置。 我的 POJO 被简单地注释为@XmlRootElement 我向我的 REST 服务发送了一个 POST 请求,一切正常。 1- 我向服务发送了
我是一名优秀的程序员,十分优秀!