golang xml marshal对应的struct两种表示方法

前端之家收集整理的这篇文章主要介绍了golang xml marshal对应的struct两种表示方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


1.xml的节点名字就是struct成员的名字,如:Project.ItemGroup,成员的类型名不影响

//vcxproj.filters
type 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.filters
type 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(" ")
}
原文链接:https://www.f2er.com/go/190419.html

猜你在找的Go相关文章