Reputation: 51
I am unable to map aid
attribute of answername
tag in the following XML to Java field aid
of Answer
Class.
Any help would be appreciated. Thanks in advance.
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 Classes:
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
Upvotes: 3
Views: 1942
Reputation: 10127
As @TimurA already said, your Java classes don't match the <answername>
elements of your XML files.
If you want to keep the structure of your XML files, then you need to adapt your Java classes.
In your Answer
class remove the aid
property,
and change the type of the answername
property:
@XmlAccessorType(XmlAccessType.FIELD)
public class Answer {
private Answername answername;
private String postedby;
// + constructors, getters, setters
}
Create a separate class for Answername
:
@XmlAccessorType(XmlAccessType.FIELD)
public class Answername {
@XmlAttribute
private int aid;
@XmlValue
private String text;
// + constructors, getters, setters
}
Upvotes: 4
Reputation: 1513
In your XML, "aid" is an attribute of "answername" not the Answer object. If you don't mind changing the XML then all you have to do is move it up one level.
<?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>
If you do want to keep the same XML, then you just have to create another class "answername" with the "aid" attribute and the value element, and Answer will have that as an element.
Upvotes: 1
Reputation: 1481
Setters and getters of the aid property are not named right. Rename them to getAid and setAid respectively.
Upvotes: 0