Open DemiMarie opened 1 month ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Could you share a code snippet for how you parse the XML? Thanks.
https://go.dev/play/p/gmZ-M1l8zVp
package main
import (
"encoding/xml"
"os"
"fmt"
"strings"
)
func checkIllFormedXMLGetsError(s string) (ok bool) {
var v error
d := xml.NewDecoder(strings.NewReader(s))
d.CharsetReader = func(charset string, reader io.Reader) (io.Reader, error) { return reader, nil }
tok, err := d.RawToken()
if tok != nil || err == nil {
_, v = fmt.Printf("BAD: got a token (%#v) or no error (%#v) when decoding %q\n", tok, err, s)
} else {
_, v = fmt.Printf("GOOD: got error (%#v) on ill-formed XML (%q)\n", err, s)
ok = true
}
if v != nil {
panic(v)
}
return
}
func main() {
illFormedDocs := []string{
`<?xml standalone="yes" version="1.0"?>`,
`<?xml version="1.0"standalone="yes"?>`,
`<?xml version="1.0" a standalone="yes"?>`,
`<?xml version="1.0" dalone="yes"?>`,
`<?xml version="1.0" encoding="not valid"?>`,
`<?xml version="1.0" standalone="not valid"?>`,
}
bad := false
for _, illFormed := range(illFormedDocs) {
if !checkIllFormedXMLGetsError(illFormed) {
bad = true
}
}
if bad {
os.Exit(1)
}
}
Thanks.
cc @rsc @ianlancetaylor
Go version
Git main
Output of
go env
in your module/workspace:What did you do?
Tried to parse XML with various ill-formed XML declarations, such as the following:
Wrong order of key/value pairs:
Missing space between key/value pairs:
Junk in data:
Invalid key:
Invalid encoding:
Invalid standalone value
What did you see happen?
No error, so long as a
CharsetReader
that can handle the invalid encoding is provided.What did you expect to see?
Errors because these documents are ill-formed.