[英]Simple XML deserializing different element types with the same name
I'm trying to deserialize this snippet of XML in Java: 我正在尝试反序列化Java中的XML片段:
<anime id="16986">
<info type="Picture" src="http://~.jpg" width="141" height="200">
<img src="http://~" width="141" height="200"/>
<img src="http://~" width="318" height="450"/>
</info>
<info type="Main title" lang="EN">Long Riders!</info>
<info type="Alternative title" lang="JA">ろんぐらいだぁす!</info>
</anime>
The problem I'm running into is that the info
element either can have an inline list of img
's or it can just contain text. 我遇到的问题是info
元素可以具有img
的内联列表,也可以仅包含文本。 I was thinking of treating info
as an @Element
in my AnimeHolder class, but I can't have duplicate annotations. 我曾考虑在AnimeHolder类中将info
视为@Element
,但是不能有重复的注释。 I would also like to access the lang
attribute of info to check if it is EN or JP. 我还想访问info的lang
属性以检查它是EN还是JP。
I am using these classes to hold the deserialized data: 我正在使用这些类来保存反序列化的数据:
@Root(name="anime", strict=false)
public class AnimeHolder {
@Attribute(name="id")
private String ANNID;
@ElementList(inline=true)
private List<InfoHolder> infoList;
public String getANNID() {
return ANNID;
}
public List<InfoHolder> getInfoList() {
return infoList;
}
}
and for the info items: 对于信息项:
@Root(name="info", strict = false)
public class InfoHolder {
@ElementList(inline=true, required = false)
private List<ImgHolder> imgList;
@Attribute(name = "lang", required = false)
private String language;
public List<ImgHolder> getImgList() {
return imgList;
}
}
With Andreas' I found out I needed to look into handling mixed content. 通过Andreas,我发现我需要研究处理混合内容。 Doing some searching lead me to this solution about creating a custom Converter
. 进行一些搜索使我找到了有关创建自定义Converter
解决方案 。 After writing my own and finding out it wasn't being called, this helped sort it out. 在写完我自己的并发现没有被调用后, 这有助于对它进行梳理。 Here is my re-worked InfoHolder
class and converter: 这是我重新设计的InfoHolder
类和转换器:
@Root(name="info", strict = false)
@Convert(InfoHolder.InfoConverter.class)
public class InfoHolder {
private String englishTitle;
private String imageURL;
static class InfoConverter implements Converter<InfoHolder> {
@Override
public InfoHolder read(InputNode node) throws Exception {
String value = node.getValue();
InfoHolder infoHolder = new InfoHolder();
if (value == null){
InputNode nextNode = node.getNext();
while (nextNode != null){
String tag = nextNode.getName();
if (tag.equals("img") && nextNode.getAttribute("src") != null){
infoHolder.imageURL = nextNode.getAttribute("src").getValue();
}
nextNode= node.getNext();
}
} else {
while (node != null){
if (node.getAttribute("lang") != null){
if (node.getAttribute("lang").getValue().equals("EN")){
infoHolder.englishTitle = value;
break;
}
}
node = node.getNext();
}
}
return infoHolder;
}
@Override
public void write(OutputNode node, InfoHolder value) throws Exception {
}
}
}
I also needed to instantiate a SimpleXmlConverterFactory
with a Serializer
using AnnotationStrategy
like so: 我还需要使用AnnotationStrategy
使用Serializer
化程序实例化SimpleXmlConverterFactory
,如下所示:
SimpleXmlConverterFactory factory = SimpleXmlConverterFactory.create(new Persister(new AnnotationStrategy()));
Using a custom Converter exposed the XML nodes, which allowed me to figure out if the info
node had img
children, and if not, get the node value itself. 使用自定义的Converter公开了XML节点,这使我能够确定info
节点是否具有img
子节点,如果没有,则获取节点值本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.