当前位置: 首页>>代码示例>>Golang>>正文


Golang xml.Decoder类代码示例

本文整理汇总了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
}
开发者ID:colinmutter,项目名称:gads,代码行数:33,代码来源:campaign_criterion.go

示例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
}
开发者ID:LuxunForever,项目名称:Leftwood,代码行数:36,代码来源:lib.go

示例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
}
开发者ID:lasithasenanayake,项目名称:v6engine-deps,代码行数:31,代码来源:xmlWorkbook.go

示例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
}
开发者ID:Richardphp,项目名称:noms,代码行数:29,代码来源:lib.go

示例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
}
开发者ID:justwatchcom,项目名称:goat,代码行数:28,代码来源:wsdl.go

示例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
}
开发者ID:allan-simon,项目名称:gads,代码行数:28,代码来源:ad_group_criterion.go

示例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
}
开发者ID:zakuro9715,项目名称:aojgo,代码行数:32,代码来源:problem.go

示例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
}
开发者ID:odwrtw,项目名称:polochon,代码行数:26,代码来源:show.go

示例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
}
开发者ID:mehulsbhatt,项目名称:eticket-billing-server,代码行数:9,代码来源:transaction_interface.go

示例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
}
开发者ID:phusl,项目名称:machine,代码行数:26,代码来源:result.go

示例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")
		}
	}
}
开发者ID:raintreeinc,项目名称:ditaconvert,代码行数:27,代码来源:xmlutils.go

示例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
}
开发者ID:phusl,项目名称:machine,代码行数:27,代码来源:result.go

示例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
}
开发者ID:speedata,项目名称:publisher,代码行数:27,代码来源:commandsxml.go

示例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)
		}
	}
}
开发者ID:twstrike,项目名称:coyim,代码行数:28,代码来源:xml.go

示例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
}
开发者ID:kballard,项目名称:voidbot,代码行数:29,代码来源:youtube.go


注:本文中的encoding/xml.Decoder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。