drl971115 2019-03-28 02:05
浏览 125

Prolog不会进行XML解析

Recently I starting using Go. I am facing one problem while parsing XML.

Here is the issue:

I am successfully able to parse the following XML:

<Root>
<cookie name="e1">hsdhsdhs</cookie>
<cookie name="e2">sssss</cookie>
<cookie name="e3">null</cookie>
<info>
<name>sam</name>
</info>
</Root>

Here are the structs:

type Profile struct {
    RootElement xml.Name    `xml:"Root"`
    CookieList  []Cookie    `xml:"cookie"`
    Info        Information `xml:"info"`
}

type Cookie struct {
    Name  string `xml:"name,attr"`
    Value string `xml:",chardata"`
}

type Information struct {
    Name       string `xml:"name"`
}

And the above struct is working fine.

profile := Profile{}
xml.Unmarshal([]byte(xmlString), &profile)
jsonData, _ := json.Marshal(profile)
fmt.Println(string(jsonData))

But as I keep prolog in XML:

<?xml version="1.0" encoding="EUC-JP"?>
    <Root>
    <cookie name="e1">hsdhsdhs</cookie>
    <cookie name="e2">sssss</cookie>
    <cookie name="e3">null</cookie>
    <info>
    <name>sam</name>
    </info>
    </Root>

then while printing, no data is displaying inside the JSON.

Not sure what is the issue here with Prolog.

  • 写回答

1条回答 默认 最新

  • duanlei7101 2019-03-28 16:20
    关注

    Before parsing non-utf8 xml document you have to difine charset reader, thanks to golang.org/x/net/html/charset all you need to do is just replace this string:

    xml.Unmarshal([]byte(xmlString), &profile)
    

    with:

    decoder := xml.NewDecoder(bytes.NewBufferString(xmlString))
    decoder.CharsetReader = charset.NewReaderLabel
    err := decoder.Decode(&profile)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记