1.xml的节点名字就是struct成员的名字,如:Project.ItemGroup,成员的类型名不影响
//vcxproj.filterstype Project struct { Toolsversion string `xml:"Toolsversion,attr"` Xmlns string `xml:"xmlns,attr"` ItemGroup []FilterGroup //`xml:"ItemGroup"`}type FilterGroup struct { Filter []Filter //`xml:"Filter"`}type Filter struct { Include string `xml:"Include,attr"` UniqueIDentifIEr string}
<?xml version="1.0" enCoding="UTF-8"?><Project Toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="pub"> <UniqueIDentifIEr>{470d3840-f2c2-4c5b-430f-e9d745bcc309}</UniqueIDentifIEr> </Filter> <Filter Include="src"> <UniqueIDentifIEr>{446a4d3d-d7e0-4ad9-7cf9-917fbba3cf8b}</UniqueIDentifIEr> </Filter> <Filter Include="pub\base"> <UniqueIDentifIEr>{1281d07c-e739-4f48-502d-54211f00ae19}</UniqueIDentifIEr> </Filter> <Filter Include="pub\driver"> <UniqueIDentifIEr>{d43ac9c6-a8f0-4484-5811-927daab37970}</UniqueIDentifIEr> </Filter> <Filter Include="pub\protocol"> <UniqueIDentifIEr>{35a150d1-33f3-4a98-5f0b-bb71982506ec}</UniqueIDentifIEr> </Filter> <Filter Include="pub\st_lib"> <UniqueIDentifIEr>{3e64a85e-249d-4a03-521e-4b5813b40edd}</UniqueIDentifIEr> </Filter> <Filter Include="pub\startup"> <UniqueIDentifIEr>{6cd70ce7-0c9c-4582-60e7-9e9b420aa43a}</UniqueIDentifIEr> </Filter> <Filter Include="pub\w5500"> <UniqueIDentifIEr>{bafafc00-7fb5-4ccb-4b30-5511fe90a3cd}</UniqueIDentifIEr> </Filter> </ItemGroup></Project>
2. xml节点的名字是struct的tag名字
xml的ItemGroup使用`xml:"ItemGroup"`标注的方法,注意只需要给slice的名字标注就可以了,具体的slice类型名字随便取
//vcxproj.filterstype Project struct { Toolsversion string `xml:"Toolsversion,attr"` FilterGroups []FilterGroup `xml:"ItemGroup"`}type FilterGroup struct { Filter []Filter //`xml:"Filter"`}type Filter struct { Include string `xml:"Include,attr"` UniqueIDentifIEr string}
<?xml version="1.0" enCoding="UTF-8"?><Project Toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="pub"> <UniqueIDentifIEr>{a0e40766-d234-4847-4772-a9b08d2fb21e}</UniqueIDentifIEr> </Filter> <Filter Include="src"> <UniqueIDentifIEr>{7fa4c884-635d-4e65-5bf9-c692ba305c9e}</UniqueIDentifIEr> </Filter> <Filter Include="pub\base"> <UniqueIDentifIEr>{e4821dfa-f228-4150-58fa-4bee90398973}</UniqueIDentifIEr> </Filter> <Filter Include="pub\driver"> <UniqueIDentifIEr>{f00ff500-af7f-4c11-703f-3e5f17e5c74b}</UniqueIDentifIEr> </Filter> <Filter Include="pub\protocol"> <UniqueIDentifIEr>{26776cca-98bd-4b46-56f8-882a0aa73cd7}</UniqueIDentifIEr> </Filter> <Filter Include="pub\st_lib"> <UniqueIDentifIEr>{d3a26e89-26d5-4d33-7a87-aea134553ac8}</UniqueIDentifIEr> </Filter> <Filter Include="pub\startup"> <UniqueIDentifIEr>{460fb330-ba31-4d5f-7060-b4719e560dc9}</UniqueIDentifIEr> </Filter> <Filter Include="pub\w5500"> <UniqueIDentifIEr>{bc43b180-e95a-4ed2-7031-5bc686e65132}</UniqueIDentifIEr> </Filter> </ItemGroup></Project>
以上测试使用的代码(注意两种情况下struct的成员名字变化了需要简单的修改下代码)
func TestFilters(t *testing.T) { var prj Project prj.Toolsversion = "4.0" prj.Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" prj.ItemGroup = make([]FilterGroup,1) filter_strs := []string{"pub","src","pub\base","pub\driver","pub\protocol","pub\st_lib","pub\startup","pub\w5500"} filters := make([]Filter,len(filter_strs)) prj.ItemGroup[0].Filter = filters for i := 0; i < len(filters); i++ { f := &filters[i] f.Include = filter_strs[i] uID,_ := uuID.NewV4() f.UniqueIDentifIEr = fmt.Sprintf("{%s}",uID.String()) } data,err := xml.Marshalindent(&prj,"","\t") if err != nil { fmt.Println(err) return } fmt.Print(xml.header) fmt.Println(string(data)) t.Error(" ")}总结
以上是内存溢出为你收集整理的golang xml marshal对应的struct两种表示方法全部内容,希望文章能够帮你解决golang xml marshal对应的struct两种表示方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)