1 回答
TA贡献1796条经验 获得超4个赞
您可以改用xml.Decoder
: Example, which severely lacks error handling。
package main
import (
"encoding/xml"
"fmt"
"io"
"strings"
)
func main() {
data := `<modules>
<elt1><version>1.2.3</version></elt1>
<eltN><version>4.5.6</version></eltN>
</modules>`
fmt.Println(parseVersions(strings.NewReader(data)))
}
func parseVersions(s io.Reader) map[string]string {
r := make(map[string]string)
decoder := xml.NewDecoder(s)
for token, err := decoder.Token(); err == nil; token, err = decoder.Token() {
switch v := token.(type) {
case xml.StartElement:
if el := v.Name.Local; strings.HasPrefix(el, "elt") {
r[el] = parseVersion(decoder)
}
}
}
return r
}
func parseVersion(decoder *xml.Decoder) string {
token, _ := decoder.Token()
switch v := token.(type) {
case xml.StartElement:
if v.Name.Local == "version" {
cd, _ := decoder.Token()
return string(cd.(xml.CharData))
}
}
return ""
}
- 1 回答
- 0 关注
- 158 浏览
添加回答
举报