I'd like to use Go to read an XML file. The problem is that it's a bad XML file -- it doesn't conform to the spec. Here's a sample:
<?xml version="1.0" encoding="UTF-8"?>
<something abc="1" def="2">
<0 x="a"/>
<1 x="b"/>
<2 x="c"/>
<26 x="z"/>
</something>
My Go program correctly gives an error when trying to read this:
$ go run rs.go <real.xml
chardata: '
'
start: name.local='something'
start {{ something} [{{ abc} 1} {{ def} 2}]}
'abc'='1'
'def'='2'
offset=66
chardata: '
'
XML syntax error on line 3: invalid XML name: 0
exit status 1
Here's the little Go program:
package main
import (
"encoding/xml"
"fmt"
"io"
"os"
)
// <something abc="1" def="2">
type Something struct {
abc string `xml:"abc"`
def string `xml:"def"`
spots []Spot
}
// <0 x="a"/>
type Spot struct {
num int // ??
xval string `xml:"x"`
}
func main() {
dec := xml.NewDecoder(os.Stdin)
// dec.Strict = false // doesn't help <0 ...> problem
// dec.Entity = xml.HTMLEntity
for {
tok, err := dec.Token()
if err == io.EOF {
break
} else if err != nil {
fmt.Fprintf(os.Stderr, "%v
", err)
os.Exit(1)
}
switch tok := tok.(type) {
case xml.StartElement:
fmt.Printf("start: name.local='%s'
", tok.Name.Local)
fmt.Printf("start %v
", tok)
for _, a := range tok.Attr {
fmt.Printf("'%s'='%s'
", a.Name.Local, a.Value)
}
fmt.Printf("offset=%d
", dec.InputOffset())
case xml.EndElement:
fmt.Printf("end: name.local='%s'
", tok.Name.Local)
case xml.CharData:
fmt.Printf("chardata: '%s'
", tok)
case xml.Comment:
fmt.Printf("comment: '%s'
", tok)
}
}
}
Is there a Go expert out there who can help me figure out how to get Go to read this goofy XML file? Thanks!