doubi3996
2017-11-28 11:42
浏览 118

使用自定义标签解析多个XML文件

I have to parse many XML documents, I already know the logic to parse the files, but the issue is, the tag for each xml file is different.

This is what I have so far

type Data struct {
     Rows []Info `xml:"domain-google.com"` // <- This is custom and needs to change (e.g. the next xml doc tag will be `xml:"domain-facebook.com"`
}

type Info struct {
     Domain string `xml:"domain"`
 }

// example logic for parsing xml

    xmlFile, err := http.Get("somefiles.xml")
    if err != nil { fmt.Println(err) }
    var data Data
    if err = xml.NewDecoder(xmlFile.Body).Decode(&data); err != nil {
        fmt.Println(err)
    }
    for _, rows := range data.Rows {
        fmt.Println(rows.Domain)
    }

example xml documents

<response>
   <!-- This tag is what is custom every time -->
   <domain-google.com>
      <domain>google.com</domain>
   </domain-google.com>
</response>

<response>
   <!-- This tag is what is custom every time -->
   <domain-facebook.com>
      <domain>facebook.com</domain>
   </domain-facebook.com>
</response>

Any idea how to tackle this issue?

Thank you in advanced.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douji3623 2017-11-28 14:22
    最佳回答

    As XML goes, this is pretty bad; tag names should not be used for data, only tag bodies and attribute values should hold data. But, all is not lost! Per the documentation on encoding/xml, you can use the ,any tag to map a field that should contain "a sub-element that hasn't matched any of the above rules". You can use this as a sort of wildcard, to match any child element not otherwise mapped - in your case, a child element whose tag name you don't know at compile time:

    type Data struct {
        Rows []Info `xml:",any"`
    }
    

    Playground example: https://play.golang.org/p/lo6bJBRvgV

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题