Golang XML marshal corresponding struct two representation method

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


The node name of 1.xml is the name of the struct member, such as: Project.itemgroup, the member's type name does not affect

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" >&Lt Uniqueidentifier>{6cd70ce7-0c9c-4582-60e7-9e9b420aa43a}</uniqueidentifier></filter><filter Include= "pub\w5500" ><UniqueIdentifier>{bafafc00-7fb5-4ccb-4b30-5511fe90a3cd}</UniqueIdentifier> </Filter></ItemGroup></Project>



2. The name of the XML node is the tag name of the struct

XML ItemGroup uses ' xml: ' ItemGroup ' notation, note that only the name of the slice can be labeled, the specific slice type name to take

Vcxproj.filterstype Project struct {toolsversion string '        xml: ' toolsversion,attr ' ' Xmlns        string        ' xml: ' Xmlns,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" >&Lt Uniqueidentifier>{460fb330-ba31-4d5f-7060-b4719e560dc9}</uniqueidentifier></filter><filter Include= "pub\w5500" ><UniqueIdentifier>{bc43b180-e95a-4ed2-7031-5bc686e65132}</UniqueIdentifier> </Filter></ItemGroup></Project>


The above test uses the code (note that the struct's member name changes in two cases and requires a simple modification of the code below)

Func testfilters (t *testing. T) {var prj projectprj.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 = filtersfor 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 ("")}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.