FAQ
Hi,
I would like to create a XML file, that I populate with my own data
(fetched from a CSV file).
Creating the XML is not so hard, but most people read XML files and map it
to their structures.
I just need some hints how to assign variables to the created structure. It
is nested, that's why I have trouble
with the correct syntax.



package main

import (
"encoding/xml"
"fmt"
"os"
)

func main() {
type Extended_Event struct {
XMLName2 xml.Name `xml:"extended-event-descriptor"`
Language2 string `xml:"Language,attr"`
Text2 string `xml:"Text"`
}




type Short_Event struct {
XMLName xml.Name `xml:"short-event-descriptor"`
Language string `xml:",attr"`
Titel string `xml:"Name"`
Text string `xml:"Text"`
}





type Event struct {
Id string `xml:"Id,attr"`
Start string `xml:"Start,attr"`
End_Time string `xml:"End_Time,attr"`
Short_Event []Short_Event
Extended_Event []Extended_Event
}




type Service struct {
Name string `xml:"Name,attr"`
Event []Event
}




type EventInformation struct {
Typus string `xml:"Data_Set_Type,attr"`
Service []Service
}

v := &Extended_Event{}
output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
}





--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tobias S. at Sep 10, 2014 at 3:25 pm
    Okay, I think it all boils down to this:
    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chris dollin at Sep 10, 2014 at 3:32 pm
    Your []Persons{...} should be []Person{...}

    (The element type of the Persons member of the Persons struct
    is Person, not Persons.)

    Chris

    PS Is there a particular reason that you're declaring the types
    inside the function?
    On 10 September 2014 16:25, Tobias S. wrote:
    Okay, I think it all boils down to this:

    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Chris "allusive" Dollin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tobias S. at Sep 10, 2014 at 3:41 pm

    On Wednesday, September 10, 2014 5:33:21 PM UTC+2, ehedgehog wrote:
    Your []Persons{...} should be []Person{...}

    (The element type of the Persons member of the Persons struct
    is Person, not Persons.)

    Chris

    PS Is there a particular reason that you're declaring the types
    inside the function?

    On 10 September 2014 16:25, Tobias S. <tobias....@gmail.com <javascript:>>
    wrote:
    Okay, I think it all boils down to this:

    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.


    --
    Chris "allusive" Dollin
    Thank you Chris, I think this will set me on the right track. Just
    experimenting, so later I will have to put the variable declarations
    outside. But thanks for pointing this out.



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tobias S. at Sep 11, 2014 at 7:11 am

    Am Mittwoch, 10. September 2014 17:41:20 UTC+2 schrieb Tobias S.:

    On Wednesday, September 10, 2014 5:33:21 PM UTC+2, ehedgehog wrote:

    Your []Persons{...} should be []Person{...}

    (The element type of the Persons member of the Persons struct
    is Person, not Persons.)

    Chris

    PS Is there a particular reason that you're declaring the types
    inside the function?
    On 10 September 2014 16:25, Tobias S. wrote:
    Okay, I think it all boils down to this:

    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Chris "allusive" Dollin
    Thank you Chris, I think this will set me on the right track. Just
    experimenting, so later I will have to put the variable declarations
    outside. But thanks for pointing this out.
    Here is the solution to my original question:
    package main

    import (
    "encoding/xml"
    "fmt"
    "os"
    )

    type Extended_Event struct {
    XMLName2 xml.Name `xml:"extended-event-descriptor"`
    Language2 string `xml:"Language,attr"`
    Text2 string `xml:"Text"`
    }

    type Short_Event struct {
    XMLName xml.Name `xml:"short-event-descriptor"`
    Language string `xml:",attr"`
    Titel string `xml:"Name"`
    Text string `xml:"Text"`
    }

    type Event struct {
    Id string `xml:"Id,attr"`
    Start string `xml:"Start,attr"`
    End_Time string `xml:"End_Time,attr"`
    Short_Event []Short_Event
    Extended_Event []Extended_Event
    }

    type Service struct {
    Name string `xml:"Name,attr"`
    Event []Event
    }

    type EventInformation struct {
    Typus string `xml:"Data_Set_Type,attr"`
    Service []Service
    }

    func main() {

    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

    output, err := xml.MarshalIndent(v, " ", " ")
    if err != nil {
    fmt.Printf("error: %v\n", err)
    }

    os.Stdout.Write(output)

    }
    ...


    The difficult bit was this code:
    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

      And I am sure the way I format this is wrong. Maybe it would look better
    using the dot notation? Hopefully it will be useful for somebody else
    fighting with XML stuff. Here a few things I stumbled upon so far:

    1. Use capital letters for the fields you want to see as Tags or
    Attributes. If you use lower-case they will not show.
    2. Use new structs to get your attributes where you want them
    3. There are helpful examples on the "official" site
    http://golang.org/pkg/encoding/xml/. But there are well hidden :-)


    I like the precise language of the golang sites, but maybe there is a way
    to bring in more of the PHP approach to documentation.That is to say more
    examples and user comments.



    >

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Sep 11, 2014 at 12:29 pm
    I suggest using the Go Playground to share code. It makes it much easier to
    collaborate on the mailing list.

    I reformatted your code a bit and fixed some errors:
    http://play.golang.org/p/gAg13RMxWj

    Your code was not setting the tag for Extended_Event to
    "extended-event-descriptor" as you clearly intended.
    There is no need to make the XMLName field an xml.Name type. In fact this
    can lead to confusion and bugs. I use struct{}.
    Can one Event contain more than one Short_Event and more than one
    Extended_Event? If not, you should probably make those fields pointers
    instead of slices. This would make the EventInformation literal in your
    code shorter also.

    You could also make EventInformation easier to read and write with some
    simple helper functions like `service` and `eventinfo` in this example:
    http://play.golang.org/p/C0THOInPqe.

    On Thu, Sep 11, 2014 at 2:11 AM, Tobias S. wrote:



    Am Mittwoch, 10. September 2014 17:41:20 UTC+2 schrieb Tobias S.:
    On Wednesday, September 10, 2014 5:33:21 PM UTC+2, ehedgehog wrote:

    Your []Persons{...} should be []Person{...}

    (The element type of the Persons member of the Persons struct
    is Person, not Persons.)

    Chris

    PS Is there a particular reason that you're declaring the types
    inside the function?
    On 10 September 2014 16:25, Tobias S. wrote:
    Okay, I think it all boils down to this:

    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Chris "allusive" Dollin
    Thank you Chris, I think this will set me on the right track. Just
    experimenting, so later I will have to put the variable declarations
    outside. But thanks for pointing this out.
    Here is the solution to my original question:
    package main

    import (
    "encoding/xml"
    "fmt"
    "os"
    )

    type Extended_Event struct {
    XMLName2 xml.Name `xml:"extended-event-descriptor"`
    Language2 string `xml:"Language,attr"`
    Text2 string `xml:"Text"`
    }

    type Short_Event struct {
    XMLName xml.Name `xml:"short-event-descriptor"`
    Language string `xml:",attr"`
    Titel string `xml:"Name"`
    Text string `xml:"Text"`
    }

    type Event struct {
    Id string `xml:"Id,attr"`
    Start string `xml:"Start,attr"`
    End_Time string `xml:"End_Time,attr"`
    Short_Event []Short_Event
    Extended_Event []Extended_Event
    }

    type Service struct {
    Name string `xml:"Name,attr"`
    Event []Event
    }

    type EventInformation struct {
    Typus string `xml:"Data_Set_Type,attr"`
    Service []Service
    }

    func main() {

    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

    output, err := xml.MarshalIndent(v, " ", " ")
    if err != nil {
    fmt.Printf("error: %v\n", err)
    }

    os.Stdout.Write(output)

    }
    ...


    The difficult bit was this code:
    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

    And I am sure the way I format this is wrong. Maybe it would look better
    using the dot notation? Hopefully it will be useful for somebody else
    fighting with XML stuff. Here a few things I stumbled upon so far:

    1. Use capital letters for the fields you want to see as Tags or
    Attributes. If you use lower-case they will not show.
    2. Use new structs to get your attributes where you want them
    3. There are helpful examples on the "official" site
    http://golang.org/pkg/encoding/xml/. But there are well hidden :-)


    I like the precise language of the golang sites, but maybe there is a way
    to bring in more of the PHP approach to documentation.That is to say more
    examples and user comments.


    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tobias S. at Sep 11, 2014 at 2:45 pm
    Thanks for your input Matt. That is looking so much better. Appreciate the
    helper functions, definitely useful!

    On Thursday, September 11, 2014 2:29:57 PM UTC+2, Matt Harden wrote:

    I suggest using the Go Playground to share code. It makes it much easier
    to collaborate on the mailing list.

    I reformatted your code a bit and fixed some errors:
    http://play.golang.org/p/gAg13RMxWj

    Your code was not setting the tag for Extended_Event to
    "extended-event-descriptor" as you clearly intended.
    There is no need to make the XMLName field an xml.Name type. In fact this
    can lead to confusion and bugs. I use struct{}.
    Can one Event contain more than one Short_Event and more than one
    Extended_Event? If not, you should probably make those fields pointers
    instead of slices. This would make the EventInformation literal in your
    code shorter also.

    You could also make EventInformation easier to read and write with some
    simple helper functions like `service` and `eventinfo` in this example:
    http://play.golang.org/p/C0THOInPqe.


    On Thu, Sep 11, 2014 at 2:11 AM, Tobias S. <tobias....@gmail.com
    <javascript:>> wrote:

    Am Mittwoch, 10. September 2014 17:41:20 UTC+2 schrieb Tobias S.:
    On Wednesday, September 10, 2014 5:33:21 PM UTC+2, ehedgehog wrote:

    Your []Persons{...} should be []Person{...}

    (The element type of the Persons member of the Persons struct
    is Person, not Persons.)

    Chris

    PS Is there a particular reason that you're declaring the types
    inside the function?
    On 10 September 2014 16:25, Tobias S. wrote:
    Okay, I think it all boils down to this:

    package main

    import (
    //"encoding/xml"
    "fmt"
    //"os"
    )

    func main() {

    type Person struct {
    Name string
    Age int
    }

    type Persons struct {
    Persons []Person
    }

    Personen := []Persons{Person{"joe", 45}}
    fmt.Println (Personen)
    }

    Sorry, but I don't understand how to assign a value to the struct Persons
    without raising errors.





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Chris "allusive" Dollin
    Thank you Chris, I think this will set me on the right track. Just
    experimenting, so later I will have to put the variable declarations
    outside. But thanks for pointing this out.
    Here is the solution to my original question:
    package main

    import (
    "encoding/xml"
    "fmt"
    "os"
    )

    type Extended_Event struct {
    XMLName2 xml.Name `xml:"extended-event-descriptor"`
    Language2 string `xml:"Language,attr"`
    Text2 string `xml:"Text"`
    }

    type Short_Event struct {
    XMLName xml.Name `xml:"short-event-descriptor"`
    Language string `xml:",attr"`
    Titel string `xml:"Name"`
    Text string `xml:"Text"`
    }

    type Event struct {
    Id string `xml:"Id,attr"`
    Start string `xml:"Start,attr"`
    End_Time string `xml:"End_Time,attr"`
    Short_Event []Short_Event
    Extended_Event []Extended_Event
    }

    type Service struct {
    Name string `xml:"Name,attr"`
    Event []Event
    }

    type EventInformation struct {
    Typus string `xml:"Data_Set_Type,attr"`
    Service []Service
    }

    func main() {

    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

    output, err := xml.MarshalIndent(v, " ", " ")
    if err != nil {
    fmt.Printf("error: %v\n", err)
    }

    os.Stdout.Write(output)

    }
    ...


    The difficult bit was this code:
    v := &EventInformation{
    "REPLACE", []Service{
    Service{"SOMESERVICE", []Event{
    Event{
    "1212", "1204", "1500", []Short_Event{
    Short_Event{xml.Name{}, "ger", "Hello", "Yes"}},
    []Extended_Event{
    Extended_Event{
    xml.Name{}, "ger", "SOMETEXT"}}}}}}}

    And I am sure the way I format this is wrong. Maybe it would look better
    using the dot notation? Hopefully it will be useful for somebody else
    fighting with XML stuff. Here a few things I stumbled upon so far:

    1. Use capital letters for the fields you want to see as Tags or
    Attributes. If you use lower-case they will not show.
    2. Use new structs to get your attributes where you want them
    3. There are helpful examples on the "official" site
    http://golang.org/pkg/encoding/xml/. But there are well hidden :-)


    I like the precise language of the golang sites, but maybe there is a way
    to bring in more of the PHP approach to documentation.That is to say more
    examples and user comments.


    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Glen Newton at Sep 12, 2014 at 6:32 am
    Just run your XML file through chidley https://github.com/gnewton/chidley and it will generate your structs for you...

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tobias S. at Sep 16, 2014 at 7:17 am
    Hey, brilliant piece of software. Even very complex XML structures are
    translated to Go structs. I wish I had seen it before painfully putting my
    together by hand.
    Thanks for sharing...

    On Friday, September 12, 2014 8:32:48 AM UTC+2, Glen Newton wrote:

    Just run your XML file through chidley https://github.com/gnewton/chidley
    and it will generate your structs for you...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Glen Newton at Sep 16, 2014 at 5:52 pm
    Thanks Tobias! Use it for your next project! :-)

    chidley now also generates Java/JAXB code (a whole maven project in fact):
    https://github.com/gnewton/chidley#user-content-java

    And I've opened an issue for generating Java code for Instagram's
    ig-json-parser : https://github.com/gnewton/chidley/issues/4
    (That is, for the json that the chidley app generates from XML, have
    chidley generate corresponding ig-json-parser Java to read this json...)

    Apologies for mentioning Java on this list! ;-)

    -Glen
    On Tuesday, September 16, 2014 3:17:43 AM UTC-4, Tobias S. wrote:

    Hey, brilliant piece of software. Even very complex XML structures are
    translated to Go structs. I wish I had seen it before painfully putting my
    together by hand.
    Thanks for sharing...

    On Friday, September 12, 2014 8:32:48 AM UTC+2, Glen Newton wrote:

    Just run your XML file through chidley https://github.com/gnewton/chidley
    and it will generate your structs for you...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 10, '14 at 1:46p
activeSep 16, '14 at 5:52p
posts10
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase