Escape sequences are not preserved
Closed this issue · 1 comments
jzelinskie commented
Hey, thanks again for pushing out the previous fixes so quickly. After fixing that, the troublesome document revealed another inconsistency: converting XML to JSON handles escape sequences, but encoding JSON to XML does not.
package main
import (
"fmt"
"github.com/clbanning/mxj/v2"
)
func main() {
xmap, _ := mxj.NewMapXml([]byte("<p>me & you</p>"), true)
jsonBytes, _ := xmap.Json()
jmap, _ := mxj.NewMapJson(jsonBytes)
newXmlBytes, _ := jmap.Xml()
fmt.Println(string(newXmlBytes))
// prints: <p>me & you</p>
// expected: <p>me & you</p>
}
clbanning commented
Unfortunately, this is a feature of the encoding/xml
decoder - https://golang.org/pkg/encoding/xml/#CharData.
You can see it in action here: https://play.golang.org/p/-UR1SmwwiwN.
However, if you toggle mxj.XMLEscapeCharsDecoder()
that should address your issue. Try -
package main
import (
"fmt"
"github.com/clbanning/mxj/v2"
)
func main() {
mxj.XMLEscapeCharsDecoder()
xmap, _ := mxj.NewMapXml([]byte("<p>me & you</p>"), true)
jsonBytes, _ := xmap.Json()
jmap, _ := mxj.NewMapJson(jsonBytes)
newXmlBytes, _ := jmap.Xml()
fmt.Println(string(newXmlBytes))
//prints: <p>me & you</p>
}