Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

xml - feed.Entry undefined (type Feed has no field or method Entry)

I need a little help with my code. I'm Kinda new at "go" but having a hard time resolving this getting this error message "(type Feed has no field or method Entry)" I have multiple func doing different things, and I need to create a few structs, but I'm running into this issue. maybe my "fmt.printf" statement is not correct, just need a second pair of eyes here. I'm trying to use IBM API if anyone wants to know.

package main

import (
    "bytes"
    "crypto/tls"
    "encoding/xml"
    "flag"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/cookiejar"
    "text/template"
)

//
// XML parsing structures
//

type Feed struct {
    XMLName xml.Name `xml:"feed"`
    Entries []Entry `xml:"entry"`
}
type Entry struct {
    XMLName  xml.Name  `xml:"entry"`
    Contents []Content `xml:"content"`
}

type Content struct {
    XMLName xml.Name           `xml:"content"`
    Lpar    []LogicalPartition `xml:"http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/ LogicalPartition"`
    //  Console []ManagementConsole `xml:"http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/ Managementconsole"`
}

type LogicalPartition struct {
    XMLName       xml.Name `xml:"http://www.ibm.com/xmlns/systems/power/firmware/uom/mc/2012_10/ LogicalPartition"`
    PartitionName string
    PartitionID   int
    PartitionUUID string
}
type ManagementConsole struct {
    XMLName xml.Name `xml:"ManagementConsole"`
    HMCUUID string
}

type Metadata struct {
    XMLName xml.Name `xml:"Metadata"`
}

type atom struct {
    XMLName xml.Name `xml:"Atom"`
    AtomID  string   `xml:"AtomID"`
}

//
// HTTP session struct
//

type Session struct {
    client   *http.Client
    User     string
    Password string
    url      string
}

func NewSession(user string, password string, url string) *Session {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    jar, err := cookiejar.New(nil)
    if err != nil {
        log.Fatal(err)
    }

    return &Session{client: &http.Client{Transport: tr, Jar: jar}, User: user, Password: password, url: url}
}

func (s *Session) doLogon() {

    authurl := s.url + "/rest/api/web/Logon"

    // template for login request
    logintemplate := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <LogonRequest xmlns="http://www.ibm.com/xmlns/systems/power/firmware/web/mc/2012_10/" schemaVersion="V1_1_0">
    <Metadata>
      <Atom/>
    </Metadata>
    <UserID kb="CUR" kxe="false">{{.User}}</UserID>
    <Password kb="CUR" kxe="false">{{.Password}}</Password>
  </LogonRequest>`

    tmpl := template.New("logintemplate")
    tmpl.Parse(logintemplate)
    authrequest := new(bytes.Buffer)
    err := tmpl.Execute(authrequest, s)
    if err != nil {
        log.Fatal(err)
    }

    request, err := http.NewRequest("PUT", authurl, authrequest)

    // set request headers
    request.Header.Set("Content-Type", "application/vnd.ibm.powervm.web+xml; type=LogonRequest")
    request.Header.Set("Accept", "application/vnd.ibm.powervm.web+xml; type=LogonResponse")
    request.Header.Set("X-Audit-Memento", "hmctest")

    response, err := s.client.Do(request)
    if err != nil {
        log.Fatal(err)
    } else {
        defer response.Body.Close()
        if response.StatusCode != 200 {
            log.Fatalf("Error status code: %d", response.StatusCode)
        }
    }
}

func (s *Session) lpar() {
    mgdurl := s.url + "/rest/api/uom/LogicalPartition"
    request, err := http.NewRequest("GET", mgdurl, nil)

    request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")

    response, err := s.client.Do(request)
    if err != nil {
        log.Fatal(err)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            log.Fatal(err)
        }

        if response.StatusCode != 200 {
            log.Fatalf("Error getting LPAR informations. status code: %d", response.StatusCode)
        }

        var feed Feed
        new_err := xml.Unmarshal(contents, &feed)

        if new_err != nil {
            log.Fatal(new_err)
        }

        fmt.Printf("%-10s%-40s 
", "partition", "UUID")
        for _, entry := range feed.Entries {
            for _, content := range entry.Contents {
                for _, lpar := range content.Lpar {
                    fmt.Printf("%-10s%-40s 
", lpar.PartitionName, lpar.PartitionUUID)
                }
            }
        }
    }
}

func (s *Session) getManaged() {
    mgdurl := s.url + "/rest/api/uom/ManagementConsole"
    request, err := http.NewRequest("GET", mgdurl, nil)
    request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    response, err := s.client.Do(request)
    if err != nil {
        log.Fatal(err)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            log.Fatal(err)
        }
        if response.StatusCode != 200 {
            log.Fatalf("Error getting HmcUUID informations. status code: %d", response.StatusCode)
        }
        var feed Feed
        new_err := xml.Unmarshal(contents, &feed)
        if new_err != nil {
            log.Fatal(new_err)
        }
        fmt.Printf("AtomID: %v
", feed.Entry.Content.ManagementConsole.Metadata.Atom.AtomID)
        //fmt.Printf("AtomCreated: %v
", feed.Entry.Content.ManagementConsole.Metadata.Atom.AtomCreated)

    }

}
question from:https://stackoverflow.com/questions/65651358/feed-entry-undefined-type-feed-has-no-field-or-method-entry

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

feed is of type Feed (var feed Feed). type Feed is defined as:

type Feed struct {
    XMLName xml.Name `xml:"feed"`
    Entries []Entry `xml:"entry"`
}

So it has a field Entries which is a slice of Entry.

You can access a single element in that slice by index (feed.Entries[0]) or you iterate over all entries:

for i, entry := range feed.Entries {
    fmt.Println(i, entry)
}

Note: the same for all the other slices like the field Contents in the Entry or Lpar in Content.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...