【问题标题】:jaxb @XmlIDREF @XmlID work amazinglyjaxb @XmlIDREF @XmlID 工作得非常好
【发布时间】:2013-03-26 10:57:57
【问题描述】:

jaxb @XmlIDREF @XmlID 效果惊人

我的 xml 输入是

<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>

我的元素 java 类: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;
    }

}

我的元素 java 类: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;
    }

}

所以如果我只是加载输入文件并将其保存到另一个文件 我惊奇地看到了这个: 在提供 prof id 小于 6 的元素中,prog 属性不显示并且在 java 上下文中为 null 但是 prof id 为 6 或更多的产品具有正确的 prof 属性

此事件也发生在课程属性的课程中,但发生在 9 而不是 6 正如您在下面看到我的部分输出:

...
 <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"/>
...

它没有规则,例如 jaxb 对这个输入 xml 正常工作,而不仅仅是 id 大于 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"/>

【问题讨论】:

  • 好的,它没有规则,例如 jaxb 可以正常工作:
  • 下面是一个示例,展示了 @XmlID@XmlIDREF 如何协同工作:blog.bdoughan.com/2010/10/…

标签: java jaxb annotations


【解决方案1】:

我的问题通过实现 IDResolver 解决了

【讨论】:

    猜你喜欢
    • 2010-10-01
    • 2012-01-03
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多