{"id":16612909,"url":"https://github.com/ttys3/go-xml","last_synced_at":"2025-07-13T12:42:13.592Z","repository":{"id":192574610,"uuid":"653107391","full_name":"ttys3/go-xml","owner":"ttys3","description":"fork of golang std xml package which add marshal self-closing tag support","archived":false,"fork":false,"pushed_at":"2023-06-13T14:22:48.000Z","size":89,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T15:42:38.175Z","etag":null,"topics":["go-xml-self-closing","golang","self-closing","self-closing-tag","xml"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ttys3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-13T12:21:04.000Z","updated_at":"2023-10-31T05:03:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"ce00a7db-2a9b-45b5-b5be-5ad8c8c96eb6","html_url":"https://github.com/ttys3/go-xml","commit_stats":null,"previous_names":["ttys3/go-xml"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttys3%2Fgo-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttys3%2Fgo-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttys3%2Fgo-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttys3%2Fgo-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ttys3","download_url":"https://codeload.github.com/ttys3/go-xml/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242917698,"owners_count":20206515,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go-xml-self-closing","golang","self-closing","self-closing-tag","xml"],"created_at":"2024-10-12T01:45:17.948Z","updated_at":"2025-03-10T19:47:46.342Z","avatar_url":"https://github.com/ttys3.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-xml\n\ngolang xml package which add marshal self-closing tag support\n\nthe code applied from https://go-review.googlesource.com/c/go/+/469495\n\n## Oops\n\nI found this https://twitter.com/ZeCoffee/status/766349635359211520\n\n![José Coelho @ZeCoffee golang encoding/xml works fine until you need a self-closing tag...](https://user-images.githubusercontent.com/41882455/245492933-32b95e6d-e409-4e33-9f9b-1ec61258c617.png)\n\n## usage\n\nCustom MarshalXML ref [https://pkg.go.dev/encoding/xml#Marshal](https://pkg.go.dev/encoding/xml#example-package-CustomMarshalXML)\n\nself-closing tag example:\n\n```go\nimport \"github.com/ttys3/go-xml\"\n\n// no `xml` struct tag is needed or can be used here\n// since we handle all this in `MarshalXML`\ntype Foo struct {\n\tBar     string\n\tComment string\n}\n\n// Custom XML marshaler for Foo\nfunc (i Foo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {\n\tattrs := []xml.Attr{\n\t\t{\n\t\t\tName:  xml.Name{Local: \"bar\"},\n\t\t\tValue: i.Bar,\n\t\t},\n\t\t{\n\t\t\tName:  xml.Name{Local: \"comment\"},\n\t\t\tValue: i.Comment,\n\t\t},\n\t}\n\n\t// Create a self-closing tag for Item\n\tempty := xml.EmptyElement{\n\t\tName: xml.Name{\n\t\t\tSpace: \"\",\n\t\t\tLocal: \"foo\",\n\t\t},\n\t\tAttr: attrs,\n\t}\n\n\t// can not use Encode or EncodeElement here, because they will not emit self-closing tag\n\terr := e.EncodeToken(empty)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Flush must be called since we are not using Encode or EncodeElement\n\tif err := e.Flush(); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nfunc TestSelfClodingTagFoo(t *testing.T) {\n\texpectedXML := `\u003cfoo bar=\"hello\" comment=\"world\"/\u003e`\n\n\tfoo := Foo{\n\t\tBar:     \"hello\",\n\t\tComment: \"world\",\n\t}\n\n\tmarshaledXML, err := xml.MarshalIndent(foo, \"\", \"  \")\n\tif err != nil {\n\t\tt.Fatalf(\"Failed to marshal XML: %v\", err)\n\t}\n\n\tif string(marshaledXML) != expectedXML {\n\t\tt.Errorf(\"Expected marshaled XML:\\n%s\\n\\nGot:\\n%s\", expectedXML, marshaledXML)\n\t}\n}\n```\n\n\n## related issues\n\nsee https://github.com/golang/go/issues/21399\n\nhttps://go-review.googlesource.com/c/go/+/469495\n\nhttps://go-review.googlesource.com/c/go/+/59830\n\nhttps://github.com/nemith/netconf/pull/27/files\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttys3%2Fgo-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fttys3%2Fgo-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttys3%2Fgo-xml/lists"}