Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: Unmarshal can't resolve recursive list sample #3088

Closed
dlintw opened this issue Feb 21, 2012 · 3 comments
Closed

encoding/xml: Unmarshal can't resolve recursive list sample #3088

dlintw opened this issue Feb 21, 2012 · 3 comments

Comments

@dlintw
Copy link

dlintw commented Feb 21, 2012

What steps will reproduce the problem?
1. capture the code in this description to xml_parse.go
2. go run xml_parse.go

What is the expected output?
a::b::c::nil

What do you see instead?
 :: nil

Which compiler are you using (5g, 6g, 8g, gccgo)?
go

Which operating system are you using?
archlinux x86_64

Which revision are you using?  (hg identify)
58bc8aae4abb

Please provide any additional information below.

// sample from http://research.swtch.com/godata2
package main

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

var encoded = `
   <list><value>a</value>
       <list><value>b</value>
           <list><value>c</value>
           </list>
       </list>
   </list>
`

type List struct {
    Value string
    List  *List
}

func (l *List) String() string {
    if l == nil {
        return "nil"
    }
    return l.Value + " :: " + l.List.String()
}

func main() {
    var l *List
    err := xml.Unmarshal([]byte(encoded),&l)
    if err != nil {
        log.Fatalln("xml.Unmarshal: ", err)
    }
    fmt.Println(l)
}
@dlintw
Copy link
Author

dlintw commented Feb 21, 2012

Comment 1:

Sorry, this is my fault. I should change the <list> into <List>
But, I still don't know how to extract xml with lowercase element.
If possible, could you explain a example on http://localhost:6060/doc/go1.html
Or, better on http://localhost:6060/pkg/encoding/xml

@dlintw
Copy link
Author

dlintw commented Feb 21, 2012

Comment 2:

Here is my modified code, it works.
I think this issue could closed.
// sample from http://research.swtch.com/godata2
package main
import (
    "encoding/xml"
    "fmt"
    "log"
)
var encoded = `
   <List><value>a</value>
       <List><value>b</value>
           <List><value>c</value>
           </List>
       </List>
   </List>
`
type List struct {
    Value string `xml:"value"`
    List  *List
}
func (l *List) String() string {
    if l == nil {
        return "nil"
    }
    return l.Value + " :: " + l.List.String()
}
func main() {
    var l *List
    err := xml.Unmarshal([]byte(encoded),&l)
    if err != nil {
        log.Fatalln("xml.Unmarshal: ", err)
    }
    fmt.Println(l)
}

@dsymonds
Copy link
Contributor

Comment 3:

Status changed to WorkingAsIntended.

@mikioh mikioh changed the title xml.Unmarshal can't resolve recursive list sample encoding/xml: Unmarshal can't resolve recursive list sample Jan 9, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants