使用 SimpleXmlConverterFactory 解析自关闭标签

问题描述 投票:0回答:1
android xml kotlin retrofit simple-xml-converter
1个回答
0
投票

您的错误消息表明使用简单 XML 错误在 XML 文档中映射图标元素存在问题,具体指出图标元素在模型类中使用 @Element 注释进行了多次映射。

要解决问题,您需要检查“图标”元素是否在模型类中正确映射,并且未映射多次。

您的 xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
<tv date="2024-03-20" source-info-name="Radio Times" generator-info-name="XMLTV" >
    <channel id="2003">
        <display-name lang="en">Hockey</display-name>
    </channel>
    <programme start="2024-03-19T07:00:00.000+0000" stop="2024-03-19T08:00:00.000+0000" channel="2003" >
        <title lang="en">Game</title>
        <icon src="https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=2" width="1920" height="1080" </icon>
    </programme>
</tv>

节目课

import org.simpleframework.xml.Element;

public class Programme {
    @Element(name = "title")
    private String title;

    @Element(name = "icon")
    private Icon icon;

    // Constructor, getters and setters...
}

图标模型类

import org.simpleframework.xml.Attribute;

public class Icon {
    @Attribute(name = "src")
    private String src;

    @Attribute(name = "width")
    private int width;

    @Attribute(name = "height")
    private int height;

    // Constructor
    public Icon(@Attribute(name = "src") String src,
                @Attribute(name = "width") int width,
                @Attribute(name = "height") int height) {
        this.src = src;
        this.width = width;
        this.height = height;
    }

    // Getters and setters...
}

根据 michael-kay 建议,XML 格式也已修改。

XML 语法要求与字符“&”转义为

&amp;
,以保证正确解析。您应该在 XML 文档中的任何属性值或文本内容中使用
&amp;
代替 & 字符。您可以确保 XML 文档格式良好,以便 XML 解析器可以通过正确转义 & 字符来成功解释它。

© www.soinside.com 2019 - 2024. All rights reserved.