douxiuyu2028 2015-03-08 09:38
浏览 50
已采纳

xml.Unmarshal错误:预期元素类型为<dict>但具有<plist>

I try to unmarshal a XML file using xml.Unmarshal of "encoding/xml" package.

The XML file starts like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>frames</key>
    <dict>
        <key>0</key>
        <dict>
            <key>frame</key>
            <string>{{0, 0}, {81, 145}}</string>
            <key>offset</key>
            <string>{0, 0}</string>
            <key>rotated</key>
            <false/>
            <key>sourceColorRect</key>
            <string>{{0, 0}, {80, 145}}</string>
            <key>sourceSize</key>
            <string>{81, 145}</string>
            <key>aliases</key>
            <array>

            </array>
        </dict>
        <key>1</key>

I define two structs:

// types for createfont command
type Characters struct {
    XMLName xml.Name `xml:"dict"`
    Char []string `xml:"key"`
}

type Result struct {
    Plist string `xml:"plist"`
    XMLName xml.Name `xml:"dict"`
    Keys []string `xml:"key"`
    Chars []Characters `xml:"dict"`
}

And unmarshal with this:

v := Result{}
err = xml.Unmarshal([]byte(data), &v)
if err != nil {
    fmt.Printf("error: %v", err)
    return
} else {
    fmt.Println("Result",v)
}

As result I get:

error: expected element type <dict> but have <plist>

If I remove the plist line in the XML file and in the struct it works fine.

How do I have to write, that I can let it in?

  • 写回答

1条回答 默认 最新

  • duangong0690 2015-03-08 18:37
    关注

    The order of your XMLName field within your struct is irrelevant. Unmarshalling happens only with regard to the field names and the tags not order.

    By having XMLName anywhere in your type you are declaring the entire type should be of/within that XML element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵