I have a problem with an xml (its late at night and cannot see the issue). I have elements/element subcollection and I read perfectly the first element but not the two subcollection of elements.
<?xml version="1.0" encoding="UTF-8" ?>
count="1">
<myElment>
<StartDate>2009-05-25</StartDate>
<EndDate>2009-06-25</EndDate>
<asOfDate>2009-06-11T10:00:00.000</asOfDate>
<numberX>385.00</numberX>
<numberY>1550</numberY>
<numberZ>195.00</numberZ>
<mySubStructA>
<value>200</value>
<units>points</units>
</mySubStructA>
<mySubStructB id="A">
<value>1550</value>
<units>kWh</units>
</mySubStructB>
<costBucket id="A">
<value>195.00</value>
<units>US Dollar</units>
</costBucket>
</myElment>
<myElment>
<StartDate>2009-05-25</StartDate>
<EndDate>2009-06-25</EndDate>
<asOfDate>2009-06-11T10:00:00.000</asOfDate>
<numberX>385.00</numberX>
<numberY>16436</numberY>
<numberZ>145.73</numberZ>
<mySubStructA>
<value>100</value>
<units>dollars</units>
</mySubStructA>
<mySubStructB id="A">
<value>16436</value>
<units>kWh</units>
</mySubStructB>
<costBucket id="A">
<value>145.73</value>
<units>US Dollar</units>
</costBucket>
</myElment>
</myElments>
And this is my program (I replaced the names because it is for a company issue, and no want no trouble dudes :-) ):
package main
import (
"encoding/xml"
"fmt"
"os"
)
type MyElements struct {
XMLName xml.Name `xml:"myElments"`
Count int `xml:"count,attr"`
TrueUps []TrueUp
}
type MyElement struct {
XMLName xml.Name `xml:"myElment"`
StartDate string `xml:"StartDate"`
EndDate string `xml:"EndDate"`
AsOfDate string `xml:"asOfDate"`
numberX float32 `xml:"numberX"`
numberY int `xml:"numberY'`
numberZ float32 `xml:"numberZ"`
MySubStructA []MySubStructA `xml:"mySubStructA"` // this xml:blah I also tried to put it in the struct as XMLName xml.Name etc
MySubStructB []MySubStructB `xml:"mySubStructB"` // this xml:blah I also tried to put it in the struct as XMLName xml.Name etc
}
type MySubStructA struct {
Value int `xml:"value"`
Units string `xml:"units"`
}
type mySubStructB struct {
Id string `xml:"id,attr"`
Value int `xml:"value"`
Units string `xml:"units"`
}
func main() {
var bytes []byte
var myelements MyElements
Fp, err := os.Open("/home/francisco/Workspace/go/src/mycompany/ingestor/myElments.xml")
if err != nil {
fmt.Println(err)
os.Exit(0)
}
FStat, err := Fp.Stat()
bytes = make([]byte, FStat.Size())
_, err = Fp.Read(bytes)
if err != err {
fmt.Println(err)
os.Exit(0)
}
xml.Unmarshal(bytes, &myelements)
fmt.Println(myelements)
fmt.Println("Count: %i", myelements.Count)
fmt.Println("Trueups: %v", myelements.MyElements)
fmt.Println("Trueups.Count: %d", len(myelements.MyElements))
}