当您有这样的xml时:

<?xml version="1.0" encoding="UTF-8"?>
<gml:FeatureCollection>
    <gml:featureMember>
        <imkl:Foo gml:id="Foo_1">
            <imkl:Bar xlink:href="Bar_1"/>
            <key>valueFoo</key>
        </imkl:Foo>
    </gml:featureMember>
    <gml:featureMember>
        <imkl:Bar gml:id="Bar1">
            <key>valueBar</key>
        </imkl:Bar>
    </gml:featureMember>
</gml:FeatureCollection>


Foo标记包含具有Bar属性的xlink:href标记,该标记指向实际上包含需要解析的标记的标记。如何告诉杰克逊与此一起工作?

杰克逊是否完全支持此功能,还是我必须手动解析?

我试过了:

酒吧班

import javax.persistence.Id;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlID;

public class Bar{

   @Id
   @XmlID
   @XmlElement
   private String id; //should be "Bar_1"

   private String key; //should be "valueBar"

   ...
}


富班

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlIDREF;

public class Foo{

   @Id
   @XmlID
   @XmlElement
   private String id; //should be "Foo_1"

   private String key; //should be "valueFoo"

   @XmlIDREF
   @XmlElement(name = "Bar")
   private Bar bar; //Should be Bar with id "Bar_1" and key with "valueBar"

   ...
}

最佳答案

如果我理解正确,则想解析内部属性,如果是,则可以简单地使用XmlAttribute

 public static void main(String[] args) throws IOException {
    XmlMapper mapper = new XmlMapper();
    // Simplified XML
    String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "    <GebiedsinformatieLevering id=\"nl.imkl-KA0000._GebiedsinformatieLevering_19O081120-1\">\n" +
            "        <belanghebbende href=\"nl.imkl-KL1011._Belanghebbende_19O081120-1\"/>\n" +
            "    </GebiedsinformatieLevering>";
    GebiedsinformatieLevering levering = mapper.readValue(str.getBytes(), GebiedsinformatieLevering.class);
    System.out.println(levering.getBelanghebbende().getHref());
}

public static class Belanghebbende {
    @XmlAttribute
    private String href;

    // getters & setters
}

public static class GebiedsinformatieLevering {
    private String text;

    @XmlIDREF
    @XmlElement(name = "belanghebbende")
    private Belanghebbende belanghebbende;

    @XmlID
    @XmlElement
    private String id;

    // getters & setters
}


}

输出:


nl.imkl-KL1011._Belanghebbende_19O081120-1


还是您需要此链接作为另一个条目的标识符?


<imkl:Belanghebbende gml:id="nl.imkl-KL1011._Belanghebbende_19O081120-1">

07-26 02:41