gpt4 book ai didi

java - JAXB Unmarshalling - 将子元素的属性映射到 java 对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:25:41 25 4
gpt4 key购买 nike

我无法将以下 XML 中 answername 标记的 aid 属性映射到 Answer 的 Java 字段 aid > 类。

如有任何帮助,我们将不胜感激。提前致谢。

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<question id="1">
<answers>
<answername aid="101">java is a programming language</answername>
<postedby>ravi</postedby>
</answers>
<answers>
<answername aid="102">java is a platform</answername>
<postedby>john</postedby>
</answers>
<questionname>What is java?</questionname>
</question>

Java 类:

Question.java

package com.deere.ruleEngine.service;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Question {
private int id;
private String questionname;
private List<Answer> answers;
public Question() {}
public Question(int id, String questionname, List<Answer> answers) {
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getQuestionname() {
return questionname;
}
public void setQuestionname(String questionname) {
this.questionname = questionname;
}
@XmlElement
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}

Answer.java

package com.deere.ruleEngine.service;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.FIELD)
public class Answer {
@XmlAttribute(name="aid")
private int aid;
private String answername;
private String postedby;
public Answer() {}
public Answer(int id, String answername, String postedby) {
super();
this.aid = id;
this.answername = answername;
this.postedby = postedby;
}

public int getId() {
return aid;
}
public void setId(int id) {
this.aid = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}

}

Current Output:

1 What is java?
Answers:
0 java is a programming language ravi
0 java is a platform john

Desired Output:

1 What is java?
Answers:
101 java is a programming language ravi
102 java is a platform john

最佳答案

正如@TimurA 所说,您的 Java 类与 <answername> 不匹配XML 文件的元素。如果要保留 XML 文件的结构,则需要调整 Java 类。

在你的Answer类删除 aid属性(property),并更改 answername 的类型属性:

@XmlAccessorType(XmlAccessType.FIELD)
public class Answer {
private Answername answername;
private String postedby;

// + constructors, getters, setters
}

Answername 创建一个单独的类:

@XmlAccessorType(XmlAccessType.FIELD)
public class Answername {

@XmlAttribute
private int aid;

@XmlValue
private String text;

// + constructors, getters, setters
}

关于java - JAXB Unmarshalling - 将子元素的属性映射到 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43707847/

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