JAXB @XmlIDREF @XmlID工作令人惊讶 [英] jaxb @XmlIDREF @XmlID work amazingly
问题描述
JAXB @XmlIDREF @XmlID惊人的工作。
我的XML输入
<&系GT;
<课程与GT;
<当然ID =1单位=4级=本科生NAME =编程基础/>
<当然ID =2单位=3级=本科生NAME =高级编程>
< pre> 1 LT; / pre>
< /当然>
<当然ID =3单位=3级=本科生NAME =离散数学>
< pre> 1 LT; / pre>
< /当然>
<当然ID =4单位=3级=本科生NAME =数据结构/>
<当然ID =5单位=3级=本科生NAME =数据库系统>
< pre> 4℃; / pre>
&所述; pre→2&下; / $ P $差异无显着
< /当然>
<当然ID =6单位=3级=本科生NAME =因特网工程>
< pre> 4℃; / pre>
&所述; pre→2&下; / $ P $差异无显着
&所述; CO> 5℃/ CO>
< /当然>
<当然ID =7单位=3级=GRADNAME =形式化方法/>
<当然ID =8单位=3级=GRADNAME =高级网络/>
<当然ID =9单位=3级=本科生NAME =管理学基础/>
<当然ID =10单位=3级=本科生NAME =IT基础/>
<当然ID =11单位=3级=本科生NAME =多媒体/>
<当然ID =12单位=3级=本科生NAME =网络安全/>
<当然ID =13单位=3级=本科生NAME =网络学习/>
< /课程>
<&PROFS GT;
&所述;教授的firstName =RamtinID =1/>
<教授的firstName =艾哈迈德ID =2/>
&所述;教授的firstName =HeshamID =3/>
<教授的firstName =FatemehID =4/>
<教授的firstName =马哈茂德ID =5/>
<教授的firstName =MahmoudrezaID =6/>
<教授的firstName =FattanehID =7/>
<教授的firstName =AzadehID =8/>
<教授的firstName =SiamakID =9/>
< / PROFS>
<条款和GT;
&所述术语的id =1>
<产品标识=1=教授6当然=1节=1时间=1产能=10/>
<产品标识=2教授=1当然=2一节=1时间=1产能=10/>
<产品标识=3教授=9当然=3一节=1时间=2容量=10/>
<产品标识=4教授=3当然=4一节=1时间=3容量=10/>
<产品标识=5教授=1当然=7部分=1时=6容量=10/>
< /术语GT;
&所述的术语GT;
<产品标识=6教授=1当然=2一节=1时间=5容量=10/>
<产品标识=7教授=9当然=3一节=1时=6容量=10/>
<产品标识=8教授=4当然=8节=1时=6容量=10/>
<产品标识=9教授=2当然=1节=1时间=1产能=10/>
< /术语GT;
< /条款和GT;
<学生和GT;
<学生的firstName =古拉姆姓氏=Patoobaf程序=1ID =810190420>
< studyrec级=9.0发行=1状态=失败/>
< studyrec级=0.0发行=9状态=INPROGRESS/>
< /学生>
<学生的firstName =Ghamar姓氏=Aghrabparast程序=1ID =810190421>
< studyrec级=16.0发行=1状态=通过/>
< studyrec级=0.0发行=6状态=INPROGRESS/>
< studyrec级=0.0发行=7状态=INPROGRESS/>
< /学生>
< /生>
< /部门>
我对元素的java类:提供
进口javax.xml.bind.annotation *。@XmlAccessorType(XmlAccessType.FIELD)
公共类发售{
@XmlAttribute
@XmlID
私人字符串ID;
@XmlAttribute
@XmlIDREF
私人教授教授;
@XmlAttribute
@XmlIDREF
私人课程学习;
@XmlAttribute
私人弦乐部分;
@XmlAttribute
私人字符串的时间;
@XmlAttribute
私人诠释能力; 公共无效setCapacity(INT容量){
this.capacity =能力;
} 公众诠释getCapacity(){
返回能力;
} 公共无效setCourse(课程学习){
this.course =课程;
} 公共课程getCourse(){
返回课程;
} 公共无效SETID(字符串ID){
this.id = ID;
} 公共字符串的getId(){
返回ID;
} 公共无效setProf(教授教授){
this.prof =教授;
} 公共getProf教授(){
返回教授;
} 公共无效setSection(字符串部分){
this.section =部分;
} 公共字符串getSection(){
返回段;
} 公共无效的setTime(字符串时间){
this.time =时间;
} 公共字符串的getTime(){
返回时间;
} @覆盖
公共布尔等于(对象T){
如果(T的instanceof发售){
如果(((发售)T).getId()。等于(this.id)){
返回true;
}
}
返回false;
}}
我对元素的java类:教授
进口javax.xml.bind.annotation *。
@XmlAccessorType(XmlAccessType.FIELD)
公共类{教授
@XmlAttribute
私有String的firstName;
@XmlAttribute
@XmlID
私人字符串ID; 公共无效SETID(字符串ID){
this.id = ID;
} 公共无效setFirstname(字符串名字){
this.firstName =名字;
} 公共字符串的getFirstName(){
返回的firstName;
} 公共字符串的getId(){
返回ID;
}}
所以,如果我只是加载并输入文件保存到另一个文件
令人惊讶的我看到这一点:
在提供要素其中教授id是小于6中,prog属性没有示出,并且在Java上下文空
但随着6教授ID或产品的更多有教授属性正确
此事件也发生了发行过程属性,但是9,而不是6
正如你看到我下面的输出的一部分:
...
&所述;提供ID =1教授=6节=1时间=1容量=10/>
<产品标识=2一节=1时间=1产能=10/>
<产品标识=3教授=9一节=1时间=2容量=10/>
<产品标识=4一节=1时间=3容量=10/>
<产品标识=5节=1时=6容量=10/>
...
有例如JAXB没有规则可以正常使用该输入XML不只是ID大于6:
<产品标识=1教授=3当然=1节=1时间=1产能=10/>
<产品标识=2教授=3当然=2一节=1时间=1产能=10/>
<产品标识=3教授=9当然=3一节=1时间=2容量=10/>
<产品标识=4教授=8当然=4一节=1时间=3容量=10/>
<产品标识=5教授=6当然=7部分=1时=6容量=10/>
我的问题通过实施IDResolver解决
jaxb @XmlIDREF @XmlID work amazingly
my xml input is
<dept>
<courses>
<course id="1" units="4" level="UNDERGRAD" name="Fundamentals of Programming"/>
<course id="2" units="3" level="UNDERGRAD" name="Advanced Programming">
<pre>1</pre>
</course>
<course id="3" units="3" level="UNDERGRAD" name="Discrete Mathematics">
<pre>1</pre>
</course>
<course id="4" units="3" level="UNDERGRAD" name="Data Structures"/>
<course id="5" units="3" level="UNDERGRAD" name="Database Systems">
<pre>4</pre>
<pre>2</pre>
</course>
<course id="6" units="3" level="UNDERGRAD" name="Internet Engineering">
<pre>4</pre>
<pre>2</pre>
<co>5</co>
</course>
<course id="7" units="3" level="GRAD" name="Formal Methods"/>
<course id="8" units="3" level="GRAD" name="Advanced Networks"/>
<course id="9" units="3" level="UNDERGRAD" name="Fundamentals of Management"/>
<course id="10" units="3" level="UNDERGRAD" name="Fundamentals of IT"/>
<course id="11" units="3" level="UNDERGRAD" name="Multimedia"/>
<course id="12" units="3" level="UNDERGRAD" name="Network Security"/>
<course id="13" units="3" level="UNDERGRAD" name="Elearning"/>
</courses>
<profs>
<prof firstName="Ramtin" id="1"/>
<prof firstName="Ahmad" id="2"/>
<prof firstName="Hesham" id="3"/>
<prof firstName="Fatemeh" id="4"/>
<prof firstName="Mahmood" id="5"/>
<prof firstName="Mahmoudreza" id="6"/>
<prof firstName="Fattaneh" id="7"/>
<prof firstName="Azadeh" id="8"/>
<prof firstName="Siamak" id="9"/>
</profs>
<terms>
<term id="1">
<offering id="1" prof="6" course="1" section="1" time="1" capacity="10"/>
<offering id="2" prof="1" course="2" section="1" time="1" capacity="10"/>
<offering id="3" prof="9" course="3" section="1" time="2" capacity="10"/>
<offering id="4" prof="3" course="4" section="1" time="3" capacity="10"/>
<offering id="5" prof="1" course="7" section="1" time="6" capacity="10"/>
</term>
<term>
<offering id="6" prof="1" course="2" section="1" time="5" capacity="10"/>
<offering id="7" prof="9" course="3" section="1" time="6" capacity="10"/>
<offering id="8" prof="4" course="8" section="1" time="6" capacity="10"/>
<offering id="9" prof="2" course="1" section="1" time="1" capacity="10"/>
</term>
</terms>
<students>
<student firstName="Gholam" lastName="Patoobaf" program="1" id="810190420">
<studyrec grade="9.0" offering="1" status="FAILED"/>
<studyrec grade="0.0" offering="9" status="INPROGRESS"/>
</student>
<student firstName="Ghamar" lastName="Aghrabparast" program="1" id="810190421">
<studyrec grade="16.0" offering="1" status="PASSED"/>
<studyrec grade="0.0" offering="6" status="INPROGRESS"/>
<studyrec grade="0.0" offering="7" status="INPROGRESS"/>
</student>
</students>
</dept>
my java class for element: offering
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Offering {
@XmlAttribute
@XmlID
private String id;
@XmlAttribute
@XmlIDREF
private Prof prof;
@XmlAttribute
@XmlIDREF
private Course course;
@XmlAttribute
private String section;
@XmlAttribute
private String time;
@XmlAttribute
private int capacity;
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public int getCapacity() {
return capacity;
}
public void setCourse(Course course) {
this.course = course;
}
public Course getCourse() {
return course;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setProf(Prof prof) {
this.prof = prof;
}
public Prof getProf() {
return prof;
}
public void setSection(String section) {
this.section = section;
}
public String getSection() {
return section;
}
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
@Override
public boolean equals(Object t) {
if (t instanceof Offering) {
if(((Offering)t).getId().equals(this.id)) {
return true;
}
}
return false;
}
}
my java class for element: Prof
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Prof {
@XmlAttribute
private String firstName;
@XmlAttribute
@XmlID
private String id;
public void setId(String id) {
this.id = id;
}
public void setFirstname(String firstname) {
this.firstName = firstname;
}
public String getFirstname() {
return firstName;
}
public String getId() {
return id;
}
}
so if i simply load and save the input file to another file i amazingly see this: in offering elements which the prof id is less than 6, the prog attribute is not shown and are null in java context but offerings with prof id of 6 or more have the prof attribute properly
this event also occurs for course attribute of offering but for 9 instead of 6 as you see part of my output below:
...
<offering id="1" prof="6" section="1" time="1" capacity="10"/>
<offering id="2" section="1" time="1" capacity="10"/>
<offering id="3" prof="9" section="1" time="2" capacity="10"/>
<offering id="4" section="1" time="3" capacity="10"/>
<offering id="5" section="1" time="6" capacity="10"/>
...
it has no rule for example jaxb works properly for this input xml not just for id greater than 6:
<offering id="1" prof="3" course="1" section="1" time="1" capacity="10"/>
<offering id="2" prof="3" course="2" section="1" time="1" capacity="10"/>
<offering id="3" prof="9" course="3" section="1" time="2" capacity="10"/>
<offering id="4" prof="8" course="4" section="1" time="3" capacity="10"/>
<offering id="5" prof="6" course="7" section="1" time="6" capacity="10"/>
my problem resolved by implementing an IDResolver
这篇关于JAXB @XmlIDREF @XmlID工作令人惊讶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!