本文整理汇总了Golang中encoding/xml.Decoder类的典型用法代码示例。如果您正苦于以下问题:Golang Decoder类的具体用法?Golang Decoder怎么用?Golang Decoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Decoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UnmarshalXML
func (ccs *CampaignCriterions) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
cc := NegativeCampaignCriterion{}
for token, err := dec.Token(); err == nil; token, err = dec.Token() {
if err != nil {
return err
}
switch start := token.(type) {
case xml.StartElement:
switch start.Name.Local {
case "campaignId":
if err := dec.DecodeElement(&cc.CampaignId, &start); err != nil {
return err
}
case "criterion":
criterion, err := criterionUnmarshalXML(dec, start)
if err != nil {
return err
}
cc.Criterion = criterion
case "bidModifier":
if err := dec.DecodeElement(&cc.BidModifier, &start); err != nil {
return err
}
case "isNegative":
if err := dec.DecodeElement(&cc.IsNegative, &start); err != nil {
return err
}
}
}
}
*ccs = append(*ccs, cc)
return nil
}
示例2: readSheetsFromZipFile
// readSheetsFromZipFile is an internal helper function that loops
// over the Worksheets defined in the XSLXWorkbook and loads them into
// Sheet objects stored in the Sheets slice of a xlsx.File struct.
func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]string) ([]*Sheet, []string, error) {
var workbook *xlsxWorkbook
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
var sheetCount int
workbook = new(xlsxWorkbook)
rc, error = f.Open()
if error != nil {
return nil, nil, error
}
decoder = xml.NewDecoder(rc)
error = decoder.Decode(workbook)
if error != nil {
return nil, nil, error
}
sheetCount = len(workbook.Sheets.Sheet)
sheets := make([]*Sheet, sheetCount)
names := make([]string, sheetCount)
sheetChan := make(chan *indexedSheet, sheetCount)
for i, rawsheet := range workbook.Sheets.Sheet {
go readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap)
}
for j := 0; j < sheetCount; j++ {
sheet := <-sheetChan
if sheet.Error != nil {
return nil, nil, sheet.Error
}
sheets[sheet.Index] = sheet.Sheet
names[sheet.Index] = workbook.Sheets.Sheet[sheet.Index].Name
}
return sheets, names, nil
}
示例3: getWorksheetFromSheet
// getWorksheetFromSheet() is an internal helper function to open a
// sheetN.xml file, refered to by an xlsx.xlsxSheet struct, from the XLSX
// file and unmarshal it an xlsx.xlsxWorksheet struct
func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, sheetXMLMap map[string]string) (*xlsxWorksheet, error) {
var rc io.ReadCloser
var decoder *xml.Decoder
var worksheet *xlsxWorksheet
var error error
var sheetName string
worksheet = new(xlsxWorksheet)
sheetName, ok := sheetXMLMap[sheet.Id]
if !ok {
if sheet.SheetId != "" {
sheetName = fmt.Sprintf("sheet%s", sheet.SheetId)
} else {
sheetName = fmt.Sprintf("sheet%s", sheet.Id)
}
}
f := worksheets[sheetName]
rc, error = f.Open()
if error != nil {
return nil, error
}
decoder = xml.NewDecoder(rc)
error = decoder.Decode(worksheet)
if error != nil {
return nil, error
}
return worksheet, nil
}
示例4: readSharedStringsFromZipFile
// readSharedStringsFromZipFile() is an internal helper function to
// extract a reference table from the sharedStrings.xml file within
// the XLSX zip file.
func readSharedStringsFromZipFile(f *zip.File) (*RefTable, error) {
var sst *xlsxSST
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
var reftable *RefTable
// In a file with no strings it's possible that
// sharedStrings.xml doesn't exist. In this case the value
// passed as f will be nil.
if f == nil {
return nil, nil
}
rc, error = f.Open()
if error != nil {
return nil, error
}
sst = new(xlsxSST)
decoder = xml.NewDecoder(rc)
error = decoder.Decode(sst)
if error != nil {
return nil, error
}
reftable = MakeSharedStringRefTable(sst)
return reftable, nil
}
示例5: UnmarshalXML
func (self *Definitions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
err = d.DecodeElement(&self.InnerDefinitions, &start)
if err != nil {
return
}
self.XMLName = start.Name
self.Aliases = map[string]string{}
self.Types.Schemas = xsd.SchemaMap{}
for _, schema := range self.Types.Schemata {
self.Types.Schemas[schema.TargetNamespace] = schema
}
for _, attr := range start.Attr {
if _, ok := self.Aliases[attr.Name.Local]; !ok {
self.Aliases[attr.Name.Local] = attr.Value
}
for k := range self.Types.Schemas {
if _, ok := self.Types.Schemas[k].Aliases[attr.Name.Local]; !ok {
self.Types.Schemas[k].Aliases[attr.Name.Local] = attr.Value
}
}
}
return
}
示例6: UnmarshalXML
func (agcs *AdGroupCriterions) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
adGroupCriterionType, err := findAttr(start.Attr, xml.Name{
Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"})
if err != nil {
return err
}
switch adGroupCriterionType {
case "BiddableAdGroupCriterion":
bagc := BiddableAdGroupCriterion{}
err := dec.DecodeElement(&bagc, &start)
if err != nil {
return err
}
*agcs = append(*agcs, bagc)
case "NegativeAdGroupCriterion":
nagc := NegativeAdGroupCriterion{}
err := dec.DecodeElement(&nagc, &start)
if err != nil {
return err
}
*agcs = append(*agcs, nagc)
default:
if StrictMode {
return fmt.Errorf("unknown AdGroupCriterion -> %#v", adGroupCriterionType)
}
}
return nil
}
示例7: UnmarshalXML
func (p *Problem) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
pp := struct {
Id string `xml:"id"`
Name string `xml:"name"`
Available string `xml:"available"`
ProblemTimeLimit string `xml:"problemtimelimit"`
ProblemMemoryLimit string `xml:"problemmemorylimit"`
Status Status `xml:"status"`
SolvedList []SolvedUser `xml:"solved_list>user"`
}{}
if err := d.DecodeElement(&pp, &start); err != nil {
return err
}
available, err := strconv.Atoi(strings.Trim(pp.Available, "\n"))
problemTimeLimit, err := strconv.Atoi(strings.Trim(pp.ProblemTimeLimit, "\n"))
problemMemoryLimit, err := strconv.Atoi(strings.Trim(pp.ProblemMemoryLimit, "\n"))
if err != nil {
return err
}
*p = Problem{
Id: strings.Trim(pp.Id, "\n"),
Name: strings.Trim(pp.Name, "\n"),
Available: available,
ProblemTimeLimit: problemTimeLimit,
ProblemMemoryLimit: problemMemoryLimit,
Status: pp.Status,
SolvedList: pp.SolvedList,
}
return nil
}
示例8: UnmarshalXML
// UnmarshalXML implements the XML Unmarshaler interface
func (s *Show) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
nfo := showFields{}
if err := d.DecodeElement(&nfo, &start); err != nil {
return err
}
s.Title = nfo.Title
s.Rating = nfo.Rating
s.Plot = nfo.Plot
s.URL = nfo.URL
s.TvdbID = nfo.TvdbID
s.ImdbID = nfo.ImdbID
s.Year = nfo.Year
if nfo.Premiered != "" {
firstAired, err := time.Parse("2006-01-02", nfo.Premiered)
if err != nil {
return err
}
s.FirstAired = &firstAired
}
return nil
}
示例9: UnmarshalXML
// SEE http://play.golang.org/p/EFXZNsjE4a and
// http://stackoverflow.com/questions/17301149/golang-xml-unmarshal-and-time-time-fields
func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
parse, _ := time.Parse(TimeFormat, v)
*c = customTime{parse}
return nil
}
示例10: getArrayValue
func getArrayValue(parser *xml.Decoder) (result interface{}, err error) {
var token xml.Token
token, err = parser.Token()
result = []interface{}{}
for {
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "value" {
var value interface{}
value, err = getValue(parser)
result = append(result.([]interface{}), value)
}
case xml.EndElement:
if t.Name.Local == "array" {
return result, err
}
}
token, err = parser.Token()
}
return
}
示例11: XMLText
func XMLText(decoder *xml.Decoder) (string, error) {
r := ""
for {
token, err := decoder.Token()
if err == io.EOF {
return r, io.EOF
}
switch token := token.(type) {
case xml.EndElement:
return r, nil
case xml.CharData:
r += string(token)
case xml.StartElement:
sub, err := XMLText(decoder)
r += sub
if err != nil {
return r, err
}
case xml.Comment: // ignore
case xml.ProcInst: // ignore
case xml.Directive: // ignore
default:
panic("unknown token")
}
}
}
示例12: getStructMember
func getStructMember(parser *xml.Decoder) (member Struct) {
var token xml.Token
token, _ = parser.Token()
member = Struct{}
for {
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "name" {
member["name"], _ = getElementValue(parser)
}
if t.Name.Local == "value" {
member["value"], _ = getValue(parser)
}
case xml.EndElement:
if t.Name.Local == "member" {
return member
}
}
token, _ = parser.Token()
}
return
}
示例13: UnmarshalXML
func (c *choice) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
for {
tok, err := dec.Token()
if err != nil && err == io.EOF {
return nil
}
if err != nil {
return err
}
switch v := tok.(type) {
case xml.StartElement:
switch v.Name.Local {
case "description":
d := &description{}
d.commands = c.commands
dec.DecodeElement(d, &v)
switch d.Lang {
case "en":
c.DescriptionEn = d
case "de":
c.DescriptionDe = d
}
}
}
}
return nil
}
示例14: nextElement
// Scan XML token stream to finc next Element (start or end)
func nextElement(p *xml.Decoder) (xml.Token, error) {
for {
t, err := p.Token()
if err != nil {
return xml.StartElement{}, err
}
switch elem := t.(type) {
case xml.StartElement, xml.EndElement:
return t, nil
case xml.CharData:
// https://xmpp.org/rfcs/rfc6120.html#xml-whitespace
// rfc6120, section 1.4: "whitespace" is used to refer to any character
// or characters matching [...] SP, HTAB, CR, or LF.
switch string(elem) {
case " ", "\t", "\r", "\n": //TODO: consider more than one whitespace
log.Println("xmpp: received whitespace ping")
}
case xml.ProcInst:
if !(elem.Target == "xml" && strings.HasPrefix(string(elem.Inst), "version=")) {
log.Printf("xmpp: received unhandled ProcInst element: target=%s inst=%s\n", elem.Target, string(elem.Inst))
}
default:
log.Printf("xmpp: received unhandled element: %#v\n", elem)
}
}
}
示例15: parseFeed
func parseFeed(d *xml.Decoder) (v Video, err error) {
var feed Feed
err = d.Decode(&feed)
if err != nil {
return
}
if len(feed.Groups) == 0 || len(feed.Groups[0].Titles) == 0 {
err = errors.New("invalid feed")
return
}
group := feed.Groups[0]
found := false
for _, title := range group.Titles {
if title.Type == "plain" {
v.Title = title.Value
found = true
break
}
}
if !found {
v.Title = group.Titles[0].Value
}
v.Duration = time.Duration(group.Duration.Value) * time.Second
return
}