我无法将以下XML中的aid
标记的answername
属性映射到aid
类的Java字段Answer
。
任何帮助将不胜感激。提前致谢。
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;
}
}
当前输出:
1 What is java?
Answers:
0 java is a programming language ravi
0 java is a platform john
期望的输出:
1 What is java?
Answers:
101 java is a programming language ravi
102 java is a platform john
答案 0 :(得分:3)
正如@TimurA所说,您的Java类与XML文件的<answername>
元素不匹配。
如果要保留XML文件的结构,则需要调整Java类。
在Answer
班级中删除aid
媒体资源
并更改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
}
答案 1 :(得分:1)
在您的XML中,&#34;援助&#34;是&#34;答案名称&#34;的属性不是答案对象。如果您不介意更改XML,那么您所要做的就是将其向上移动一层。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<question id="1">
<answers aid="101">
<answername>java is a programming language</answername>
<postedby>ravi</postedby>
</answers>
<answers aid="102">
<answername>java is a platform</answername>
<postedby>john</postedby>
</answers>
<questionname>What is java?</questionname>
</question>
如果你想保留相同的XML,那么你只需创建另一个类&#34; answername&#34;与&#34;援助&#34;属性和值元素,而答案将把它作为一个元素。
答案 2 :(得分:0)
援助财产的设定者和获取者没有被命名为正确。将它们分别重命名为 getAid 和 setAid 。