當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。