-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/xml: EncodeToken doesn't indent Comment #56340
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
Comments
Nothing happens since nov 2022. But I need this fix. Is there a workaround for this? |
Change https://go.dev/cl/564575 mentions this issue: |
Experience tells us that almost every change to the XML package breaks some existing code. Why is this change important? |
@ianlancetaylor I've tested this change in my local codebase, and it functions as expected. If you already have code that utilizes the encoding/xml package, the only difference you may notice is that when using the Indent function, comments will appear on separate lines. No other behavior is affected. |
@Faetu Thanks, but that doesn't quite answer my question. Why do comments need to be on their own lines? XML is not a lined-oriented format.
I'm not sure what you mean by "a new option." It seems to me that https://go.dev/cl/564575 will change all XML code that includes comments. It's true that the change shouldn't matter. But then, if the change doesn't matter, why make it? |
sry for my late response.
In my case, I need to present the generated XML to a human with comments in their own lines. However, there are instances where I must process provided XML and manage comments relative to the elements. To achieve the necessary handling, comments must be formatted in their own lines. In other scenarios where Python is utilized, comments are typically generated in this manner by default.
I referred to the 'new option' as the capability to insert comments in their own lines with proper indentation. With this modification, I can now not only achieve this formatting but also retain the previous behavior, where comments are placed immediately after For example: if err := xmlEncoder.EncodeToken(xml.StartElement{Name: xml.Name{Local: "MyFirstElem"}}); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.EndElement{Name: xml.Name{Local: "MyFirstElem"}}); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.Comment("Comment")); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.StartElement{Name: xml.Name{Local: "MySecElem"}}); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.EndElement{Name: xml.Name{Local: "MySecElem"}}); err != nil {
return err
} result (with this change): <MyFirstElem></MyFirstElem>
<!-- Comment -->
<MySecElem></MySecElem> For the old behaviour: if err := xmlEncoder.EncodeToken(xml.StartElement{Name: xml.Name{Local: "MyFirstElem"}}); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.EndElement{Name: xml.Name{Local: "MyFirstElem"}}); err != nil {
return err
}
// disable indent
xmlEncoder.Indent("","")
if err := xmlEncoder.EncodeToken(xml.Comment("Comment")); err != nil {
return err
}
// enable indent again
xmlEncoder.Indent(""," ")
if err := xmlEncoder.EncodeToken(xml.StartElement{Name: xml.Name{Local: "MySecElem"}}); err != nil {
return err
}
if err := xmlEncoder.EncodeToken(xml.EndElement{Name: xml.Name{Local: "MySecElem"}}); err != nil {
return err
} Result: <MyFirstElem></MyFirstElem><!-- Comment -->
<MySecElem></MySecElem>
Yes, it shouldn't matter for most cases. However, it does matter in my case, as well as in the case of @hlcfan and perhaps others. Additionally, in Python, for example, the default formatting ensures that comments are placed on a new line. With this change, we now have a convenient and straightforward method to add XML comments on separate lines, while still retaining the ability to maintain the old behavior. best regards |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
Comment is put as a new line.
What did you see instead?
Comment is put at the same line as previous element.
What to change?
I think we can add
p.writeIndent(1)
andp.writeIndent(-1)
before and after writing the comment element, https://github.com/golang/go/blob/go1.19.2/src/encoding/xml/marshal.go#L220.The text was updated successfully, but these errors were encountered: