本文整理汇总了Golang中encoding/xml.Decoder.Decode方法的典型用法代码示例。如果您正苦于以下问题:Golang Decoder.Decode方法的具体用法?Golang Decoder.Decode怎么用?Golang Decoder.Decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类encoding/xml.Decoder
的用法示例。
在下文中一共展示了Decoder.Decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: readWorkbookRelationsFromZipFile
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to. The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string, error) {
var sheetXMLMap map[string]string
var wbRelationships *xlsxWorkbookRels
var rc io.ReadCloser
var decoder *xml.Decoder
var err error
rc, err = workbookRels.Open()
if err != nil {
return nil, err
}
decoder = xml.NewDecoder(rc)
wbRelationships = new(xlsxWorkbookRels)
err = decoder.Decode(wbRelationships)
if err != nil {
return nil, err
}
sheetXMLMap = make(map[string]string)
for _, rel := range wbRelationships.Relationships {
if strings.HasSuffix(rel.Target, ".xml") && strings.HasPrefix(rel.Target, "worksheets/") {
sheetXMLMap[rel.Id] = strings.Replace(rel.Target[len("worksheets/"):], ".xml", "", 1)
}
}
return sheetXMLMap, nil
}
示例4: 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
}
示例5: 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) ([]*Sheet, []string, error) {
var workbook *xlsxWorkbook
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
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
}
sheets := make([]*Sheet, len(workbook.Sheets.Sheet))
names := make([]string, len(workbook.Sheets.Sheet))
for i, rawsheet := range workbook.Sheets.Sheet {
worksheet, error := getWorksheetFromSheet(rawsheet, file.worksheets)
if error != nil {
return nil, nil, error
}
sheet := new(Sheet)
sheet.Rows, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, file)
sheets[i] = sheet
names[i] = rawsheet.Name
}
return sheets, names, nil
}
示例6: 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
}
示例7: readWorkbookRelationsFromZipFile
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to. The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
var sheetXMLMap WorkBookRels
var wbRelationships *xlsxWorkbookRels
var rc io.ReadCloser
var decoder *xml.Decoder
var err error
rc, err = workbookRels.Open()
if err != nil {
return nil, err
}
decoder = xml.NewDecoder(rc)
wbRelationships = new(xlsxWorkbookRels)
err = decoder.Decode(wbRelationships)
if err != nil {
return nil, err
}
sheetXMLMap = make(WorkBookRels)
for _, rel := range wbRelationships.Relationships {
if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
_, filename := path.Split(rel.Target)
sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
}
}
return sheetXMLMap, nil
}
示例8: parsePage
func parsePage(d *xml.Decoder, ch chan<- *wikiparse.Page) error {
page := wikiparse.Page{}
err := d.Decode(&page)
if err != nil {
return err
}
ch <- &page
return nil
}
示例9: 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) (map[string]*Sheet, []*Sheet, error) {
var workbook *xlsxWorkbook
var err error
var rc io.ReadCloser
var decoder *xml.Decoder
var sheetCount int
workbook = new(xlsxWorkbook)
rc, err = f.Open()
if err != nil {
return nil, nil, err
}
decoder = xml.NewDecoder(rc)
err = decoder.Decode(workbook)
if err != nil {
return nil, nil, err
}
file.Date1904 = workbook.WorkbookPr.Date1904
for entryNum := range workbook.DefinedNames.DefinedName {
file.DefinedNames = append(file.DefinedNames, &workbook.DefinedNames.DefinedName[entryNum])
}
// Only try and read sheets that have corresponding files.
// Notably this excludes chartsheets don't right now
var workbookSheets []xlsxSheet
for _, sheet := range workbook.Sheets.Sheet {
if f := worksheetFileForSheet(sheet, file.worksheets, sheetXMLMap); f != nil {
workbookSheets = append(workbookSheets, sheet)
}
}
sheetCount = len(workbookSheets)
sheetsByName := make(map[string]*Sheet, sheetCount)
sheets := make([]*Sheet, sheetCount)
sheetChan := make(chan *indexedSheet, sheetCount)
defer close(sheetChan)
go func() {
err = nil
for i, rawsheet := range workbookSheets {
readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap)
}
}()
for j := 0; j < sheetCount; j++ {
sheet := <-sheetChan
if sheet.Error != nil {
return nil, nil, sheet.Error
}
sheetName := workbookSheets[sheet.Index].Name
sheetsByName[sheetName] = sheet.Sheet
sheet.Sheet.Name = sheetName
sheets[sheet.Index] = sheet.Sheet
}
return sheetsByName, sheets, nil
}
示例10: 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) (map[string]*Sheet, []*Sheet, error) {
var workbook *xlsxWorkbook
var err error
var rc io.ReadCloser
var decoder *xml.Decoder
var sheetCount int
workbook = new(xlsxWorkbook)
rc, err = f.Open()
if err != nil {
return nil, nil, err
}
decoder = xml.NewDecoder(rc)
err = decoder.Decode(workbook)
if err != nil {
return nil, nil, err
}
file.Date1904 = workbook.WorkbookPr.Date1904
sheetCount = len(workbook.Sheets.Sheet)
sheetsByName := make(map[string]*Sheet, sheetCount)
sheets := make([]*Sheet, sheetCount)
sheetChan := make(chan *indexedSheet, sheetCount)
defer close(sheetChan)
go func() {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
result := &indexedSheet{Index: -1, Sheet: nil, Error: err}
sheetChan <- result
}
}()
err = nil
for i, rawsheet := range workbook.Sheets.Sheet {
readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap)
}
}()
for j := 0; j < sheetCount; j++ {
sheet := <-sheetChan
if sheet.Error != nil {
return nil, nil, sheet.Error
}
sheetName := workbook.Sheets.Sheet[sheet.Index].Name
sheetsByName[sheetName] = sheet.Sheet
sheet.Sheet.Name = sheetName
sheets[sheet.Index] = sheet.Sheet
}
return sheetsByName, sheets, nil
}
示例11: readData
func (d *Decoder) readData(db *Database) error {
masterKey, err := db.Credentials.buildMasterKey(db)
if err != nil {
return err
}
block, err := aes.NewCipher(masterKey)
if err != nil {
return err
}
in, err := ioutil.ReadAll(d.r)
if err != nil {
return err
}
mode := cipher.NewCBCDecrypter(block, db.Headers.EncryptionIV)
decrypted := make([]byte, len(in))
mode.CryptBlocks(decrypted, in)
startBytes := db.Headers.StreamStartBytes
if !reflect.DeepEqual(decrypted[0:len(startBytes)], startBytes) {
return errors.New("Database integrity check failed")
}
decrypted = decrypted[len(startBytes):]
var xmlDecoder *xml.Decoder
if db.Headers.CompressionFlags == GzipCompressionFlag { //Unzip if the header compression flag is 1 for gzip
zippedBody, err := checkHashBlocks(decrypted)
if err != nil {
return err
}
b := bytes.NewBuffer(zippedBody)
r, err := gzip.NewReader(b)
if err != nil {
return err
}
defer r.Close()
xmlDecoder = xml.NewDecoder(r)
} else { //Otherwise assume it not compressed
xmlDecoder = xml.NewDecoder(bytes.NewReader(decrypted))
}
db.Content = &DBContent{}
err = xmlDecoder.Decode(db.Content)
return err
}
示例12: readStylesFromZipFile
// readStylesFromZipFile() is an internal helper function to
// extract a style table from the style.xml file within
// the XLSX zip file.
func readStylesFromZipFile(f *zip.File) (*xlsxStyles, error) {
var style *xlsxStyles
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
rc, error = f.Open()
if error != nil {
return nil, error
}
style = new(xlsxStyles)
decoder = xml.NewDecoder(rc)
error = decoder.Decode(style)
if error != nil {
return nil, error
}
return style, nil
}
示例13: readStylesFromZipFile
// readStylesFromZipFile() is an internal helper function to
// extract a style table from the style.xml file within
// the XLSX zip file.
func readStylesFromZipFile(f *zip.File, theme *theme) (*xlsxStyleSheet, error) {
var style *xlsxStyleSheet
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
rc, error = f.Open()
if error != nil {
return nil, error
}
style = newXlsxStyleSheet(theme)
decoder = xml.NewDecoder(rc)
error = decoder.Decode(style)
if error != nil {
return nil, error
}
buildNumFmtRefTable(style)
return style, nil
}
示例14: subscribe
func subscribe(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, fullJID string) error {
var bareJID string
if barePosition := strings.Index(fullJID, "/"); barePosition < 0 {
return fmt.Errorf("Can't split JID %s", fullJID)
} else {
bareJID = fullJID[:barePosition]
}
var subscribe struct {
XMLName xml.Name `xml:"jabber:client iq"`
Type string `xml:"type,attr"`
To string `xml:"to,attr"`
ID string `xml:"id,attr"`
Subscribe struct {
XMLName xml.Name `xml:"google:push subscribe"`
Item struct {
XMLName xml.Name `xml:"item"`
Channel string `xml:"channel,attr"`
From string `xml:"from,attr"`
}
}
}
subscribe.Type = "set"
subscribe.To = bareJID
subscribe.ID = "3"
subscribe.Subscribe.Item.Channel = "cloudprint.google.com"
subscribe.Subscribe.Item.From = "cloudprint.google.com"
if err := xmlEncoder.Encode(&subscribe); err != nil {
return fmt.Errorf("XMPP subscription request failed: %s", err)
}
var subscription struct {
XMLName xml.Name `xml:"jabber:client iq"`
To string `xml:"to,attr"`
From string `xml:"from,attr"`
}
if err := xmlDecoder.Decode(&subscription); err != nil {
return fmt.Errorf("XMPP subscription response invalid: %s", err)
} else if fullJID != subscription.To || bareJID != subscription.From {
return errors.New("XMPP subscription failed")
}
return nil
}
示例15: 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) ([]string, error) {
var sst *xlsxSST
var error error
var rc io.ReadCloser
var decoder *xml.Decoder
var reftable []string
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
}