当你有这样的 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
标记包含一个带有 xlink:href
属性的 Bar
标记,该标记指向实际包含以下标记的标记:需要解析。如何告诉 jackson 使用这个?
jackson 是否支持开箱即用,或者我必须手动解析它?
我尝试过:
酒吧类
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"
...
}
Foo 类
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">
关于java - 如何使用xlink序列化xml标签:href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432990/