如何从 marshal 重新排序 xml 标签

我正在导出 XML 的程序似乎希望 xml 标签按特定顺序排列,如下例所示


<xml>

  <tagType1>data 1</tagType1>

  <tagType2>data 2</tagType2>

  <tagType1>data 3</tagType1>

  <tagType2>data 4</tagType2>

</xml>

在 go 中,我编组为如下所示的结构


type xml struct {

  TagType1 []string `xml:"tagType1"`

  TagType2 []string `xml:"tagType2"`

}

当我将其整理出来时,它会对预期的标签进行排序,但这不是我需要的。


<xml>

  <tagType1>data 1</tagType1>

  <tagType1>data 3</tagType1>

  <tagType2>data 2</tagType2>

  <tagType2>data 4</tagType2>

</xml>

有没有办法使用encoding/xml 包来重现第一个示例中的输出?顺序有所不同。我读取具有特定命令的 xml 文件,修改数据并编组返回。我需要保留标签顺序。


叮当猫咪
浏览 60回答 1
1回答

UYOU

你应该能够使用这样的东西:type xml struct {&nbsp; &nbsp;Item []ItemStruct `xml:",any"`}type ItemStruct struct {&nbsp; XMLName xml.Name&nbsp; Value string `xml:",chardata"`}这样你就可以保持顺序,但是你需要通过以下方式从每个项目中获取元素名称Item[i].XMLName
打开App,查看更多内容
随时随地看视频慕课网APP