如何使用@XmlIDREF 和@XmlID [英] How to use @XmlIDREF and @XmlID

查看:18
本文介绍了如何使用@XmlIDREF 和@XmlID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白 @XmlIDREF@XmlID 如何协同工作.通过使用 XmlIDREF,我只创建了对实际元素的引用.但是,XmlID 的用例是什么?

I don't quite understand how @XmlIDREF and @XmlID work together. By using XmlIDREF I only create a reference to the actual element. However what is the use case for XmlID.

我想创建对 Publication 类的引用.用 @XmlIDREF 注释发布 List 就够了吗?

I want to create a reference to the class Publication. Is it enough to annotate the publication List with @XmlIDREF?

public class Author {

    private String id;
    private String name;
    private List<Publication> publications = new LinkedList<>();

    public Author() {
        super();
    }
    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    @XmlIDREF
    public List<Publication> getPublications() {
        return publications;
    }

推荐答案

我想创建对 Publication 类的引用.用 @XmlIDREF 注释发布 List 就够了吗?

I want to create a reference to the class Publication. Is it enough to annotate the publication List with @XmlIDREF?

不,这只是您需要的一半.

No, that's only one half of what you need.

  • 你已经有了这个:使用 @XmlIDREF 标记关系的引用端(从 Author 指向 Publication).

  • You already have this: With @XmlIDREF you mark the referencing side of the relation (pointing from Author to Publication).

public class Author {

    ...

    @XmlIDREF
    @XmlElement(name = "publication")
    public List<Publication> getPublications() {
        return publications;
    }

    ...

}

  • 您还需要标记引用一侧(Publication 本身)通过使用 @XmlID 注释其属性之一,例如像这样:

  • You also need to mark the referenced side (the Publication itself) by annotating one of its properties with @XmlID, for example like this:

    public class Publication {
    
        ...
    
        @XmlID
        @XmlElement
        public String getId() {
            return id;
        }
    
        ...
    }
    

  • 然后你就可以像这个例子一样处理 XML 内容了:

    Then you are able to process XML content like this example:

    <root>
        <publication>
           <id>p-101</id>
           <title>Death on the Nile</title>
        </publication> 
        <publication>
           <id>p-102</id>
           <title>The murder of Roger Ackroyd</title>
        </publication> 
        ...
        <author>
           <id>a-42</id>
           <name>Agatha Christie</name>
           <publication>p-101</publication>
           <publication>p-102</publication>
        </author>
        ...
    </root>
    

    你看,XML 引用(比如 p-101)映射到 Java 对象引用(在 Listpublications 中).

    You see, the XML references (like <publication>p-101</publication>) are mapped to Java object references (in List<Publication> publications).

    这篇关于如何使用@XmlIDREF 和@XmlID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆