...
1
2
3
4
5 package junit
6
7 import "encoding/xml"
8
9 type xmlNode struct {
10 XMLName xml.Name
11 Attrs map[string]string `xml:"-"`
12 Content []byte `xml:",innerxml"`
13 Nodes []xmlNode `xml:",any"`
14 }
15
16 func (n *xmlNode) Attr(name string) string {
17 return n.Attrs[name]
18 }
19
20 func (n *xmlNode) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
21 type nodeAlias xmlNode
22 if err := d.DecodeElement((*nodeAlias)(n), &start); err != nil {
23 return err
24 }
25
26 content, err := extractContent(n.Content)
27 if err != nil {
28 return err
29 }
30
31 n.Content = content
32
33 n.Attrs = attrMap(start.Attr)
34 return nil
35 }
36
37 func attrMap(attrs []xml.Attr) map[string]string {
38 if len(attrs) == 0 {
39 return nil
40 }
41
42 attributes := make(map[string]string, len(attrs))
43 for _, attr := range attrs {
44 attributes[attr.Name.Local] = attr.Value
45 }
46 return attributes
47 }
48
View as plain text