We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
If I want to indent some JSON bytes, I can use the helpful json.Indent:
json.Indent
https://godocs.io/encoding/json#Indent
However XML does not have a similar function, so you are forced to do a roundtrip:
package indent import "encoding/xml" func indent_XML(src []byte) ([]byte, error) { type node struct { Children []node `xml:",any"` Text string `xml:",chardata"` XMLName xml.Name } var dst node err := xml.Unmarshal(src, &dst) if err != nil { return nil, err } return xml.MarshalIndent(dst, "", " ") }
and even then, it seems the result is not valid:
<regionalRating> <rating>TV-PG</rating> <region>CA</region>
 
 
 </regionalRating>
I think it would be helpful if an xml.Indent was available.
xml.Indent
The text was updated successfully, but these errors were encountered:
here is an implementation that might work:
package xml import ( "bytes" "encoding/xml" "io" ) func Indent(dst io.Writer, src io.Reader, prefix, indent string) error { decode := xml.NewDecoder(src) encode := xml.NewEncoder(dst) encode.Indent(prefix, indent) for { token, err := decode.Token() if err == io.EOF { return encode.Flush() } if err != nil { return err } data, ok := token.(xml.CharData) if ok { token = xml.CharData(bytes.TrimSpace(data)) } if err := encode.EncodeToken(token); err != nil { return err } } }
Sorry, something went wrong.
No branches or pull requests
If I want to indent some JSON bytes, I can use the helpful
json.Indent
:https://godocs.io/encoding/json#Indent
However XML does not have a similar function, so you are forced to do a roundtrip:
and even then, it seems the result is not valid:
I think it would be helpful if an
xml.Indent
was available.The text was updated successfully, but these errors were encountered: