GithubHelp home page GithubHelp logo

ttys3 / go-xml Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 89 KB

fork of golang std xml package which add marshal self-closing tag support

Go 100.00%
golang self-closing xml go-xml-self-closing self-closing-tag

go-xml's Introduction

go-xml

golang xml package which add marshal self-closing tag support

the code applied from https://go-review.googlesource.com/c/go/+/469495

Oops

I found this https://twitter.com/ZeCoffee/status/766349635359211520

José Coelho @ZeCoffee golang encoding/xml works fine until you need a self-closing tag...

usage

Custom MarshalXML ref https://pkg.go.dev/encoding/xml#Marshal

self-closing tag example:

import "github.com/ttys3/go-xml"

// no `xml` struct tag is needed or can be used here
// since we handle all this in `MarshalXML`
type Foo struct {
	Bar     string
	Comment string
}

// Custom XML marshaler for Foo
func (i Foo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	attrs := []xml.Attr{
		{
			Name:  xml.Name{Local: "bar"},
			Value: i.Bar,
		},
		{
			Name:  xml.Name{Local: "comment"},
			Value: i.Comment,
		},
	}

	// Create a self-closing tag for Item
	empty := xml.EmptyElement{
		Name: xml.Name{
			Space: "",
			Local: "foo",
		},
		Attr: attrs,
	}

	// can not use Encode or EncodeElement here, because they will not emit self-closing tag
	err := e.EncodeToken(empty)
	if err != nil {
		return err
	}

	// Flush must be called since we are not using Encode or EncodeElement
	if err := e.Flush(); err != nil {
		return err
	}

	return nil
}

func TestSelfClodingTagFoo(t *testing.T) {
	expectedXML := `<foo bar="hello" comment="world"/>`

	foo := Foo{
		Bar:     "hello",
		Comment: "world",
	}

	marshaledXML, err := xml.MarshalIndent(foo, "", "  ")
	if err != nil {
		t.Fatalf("Failed to marshal XML: %v", err)
	}

	if string(marshaledXML) != expectedXML {
		t.Errorf("Expected marshaled XML:\n%s\n\nGot:\n%s", expectedXML, marshaledXML)
	}
}

related issues

see golang/go#21399

https://go-review.googlesource.com/c/go/+/469495

https://go-review.googlesource.com/c/go/+/59830

https://github.com/nemith/netconf/pull/27/files

go-xml's People

Contributors

ttys3 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

go-xml's Issues

self-closing tag not correctly indendted when using `xml.MarshalIndent`

self-closing tag not correctly indendted when using xml.MarshalIndent

it generate something like:

        <DATASET name="fooo">
          <DATA>
            <ITEM key="key1" val="xxx"/>
              <ITEM key="key2" val="xxx"/>
                <ITEM key="key3" val="xxx"/>
                  <ITEM key="key4" val="xxx"/></DATA>
                </DATASET>

not exptected:

<DATASET name="fooo">
  <DATA>
    <ITEM key="key1" val="xxx" />
    <ITEM key="key2" val="xxx" />
    <ITEM key="key3" val="xxx" />
    <ITEM key="key4" val="xxx" />
  </DATA>
</DATASET>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.