Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: unexpected behavior of encoder.Indent("", "") #13185

Open
poffe opened this issue Nov 8, 2015 · 7 comments

Comments

Projects
None yet
6 participants
@poffe
Copy link

commented Nov 8, 2015

Marshaling xml with prefix and indent set to empty strings results in unindented xml. Tested in Go 1.5.1 darwin/amd64.

The following code:

package main

import (
    "encoding/xml"
    "fmt"
)

type Person struct {
    XMLName   xml.Name `xml:"person"`
    Id        int      `xml:"id,attr"`
    FirstName string   `xml:"name>first"`
    LastName  string   `xml:"name>last"`
    Age       int      `xml:"age"`
}

func main() {
    v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}

    output, err := xml.MarshalIndent(v, "", "")
    if err != nil {
        fmt.Printf("error: %v\n", err)
    }

    fmt.Println(string(output))
}

gives:

<person id="13"><name><first>John</first><last>Doe</last></name><age>42</age></person>

but expteded was:

<person id="13">
<name>
<first>John</first>
<last>Doe</last>
</name>
<age>42</age>
</person>

I know, not major, but believe it or not, I actually need the latter behavior. :-)

@ianlancetaylor ianlancetaylor changed the title encoder/xml: unexpected behavior of encoder.Indent("", "") encoding/xml: unexpected behavior of encoder.Indent("", "") Nov 8, 2015

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Nov 8, 2015

@ibukanov

This comment has been minimized.

Copy link

commented May 23, 2016

I also assumed that encoder.Indent("", "") should pretty-print with each element on its own line with no indent rather than disabling pretty-print. I need to work with potentially big and rather nested XML. For debugging and friendliness to diff I would like to save files with each element printed on its own line. However even with Indent("", " ") I got a significant increase in file size. I can work-around that with a regexp that strips initial spaces from each line with xml elements, but it would be nice if Indent("", "") worked as expected.

@gopherbot

This comment has been minimized.

Copy link

commented Mar 24, 2018

Change https://golang.org/cl/102417 mentions this issue: encoding/xml: unexpected behavior of encoder.Indent("", "")

@iWdGo

This comment has been minimized.

Copy link
Contributor

commented Mar 24, 2018

MarshalIndent and Marshal share the code. When prefix and indent are
empty, the behaviour is like Marshal when it should have a minimal
indent, i.e. new line.

A boolean is added to the local printer struct which defaults to false.
It is set to true only when MarshalIndent is used and prefix and indent
are empty.

I submitted a minor change to solve this issue.

@ALTree ALTree added the NeedsFix label Mar 24, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Mar 24, 2018

Change https://golang.org/cl/102420 mentions this issue: encoding/xml: fix unexpected behavior of encoder.Indent("", "")

@gopherbot

This comment has been minimized.

Copy link

commented Apr 23, 2018

Change https://golang.org/cl/108797 mentions this issue: encoding/xml: fix unexpected behavior of encoder.Indent("", "")

@iWdGo

This comment has been minimized.

Copy link
Contributor

commented Apr 23, 2018

The fix has been adapted to fix #7113

@gopherbot

This comment has been minimized.

Copy link

commented Apr 27, 2018

Change https://golang.org/cl/109855 mentions this issue: encoding/xml : Fixes to enforce XML namespace standard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.