xml.Unmarshal Question

219 views
Skip to first unread message

Francisco Diaz Trepat - gmail

unread,
Apr 26, 2012, 8:58:38 PM4/26/12
to golang-nuts
Hi all, Gustavo (so long man, hope you are awesome)

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.

I have this xml

<?xml version="1.0" encoding="UTF-8" ?>
xsi:schemaLocation="http://platform.tendrilinc.com/tnop/extension/ems extensions/ems-utility-info.xsd"
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))
}

f(t)

Gustavo Niemeyer

unread,
Apr 26, 2012, 11:46:02 PM4/26/12
to Francisco Diaz Trepat - gmail, golang-nuts
Hi Francisco,

> Hi all, Gustavo (so long man, hope you are awesome)

Thanks, it's all good here. Hope things are running well for you too.

> 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.

I know the feeling. I suggest waking up rested, getting a good cup of
tea, and having a look at it with a fresh brain. It works fine, but
you've clearly never run the code you've pasted, so I can't even tell
what you're getting wrong.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

Francisco Diaz Trepat - gmail

unread,
Apr 27, 2012, 8:12:14 AM4/27/12
to Gustavo Niemeyer, golang-nuts
:-)

so true...
Reply all
Reply to author
Forward
0 new messages