Example #1
0
func flattenXml(xmlStr string) (map[string]interface{}, error) {
	decoder := xml.NewDecoder(bytes.NewBufferString(xmlStr))
	result := make(map[string]interface{}, 0)

	buffer := bytes.NewBufferString("")
	elementStack := make([]*elementRef, 0)
	var last *elementRef
	for {
		token, err := decoder.Token()
		if err == io.EOF {
			return result, nil
		} else if err != nil {
			return nil, errors.Wrap(err, 0)
		}
		switch token.(type) {
		case xml.StartElement:
			name := token.(xml.StartElement).Name.Local
			if last != nil && last.name == name {
				last.Inc()
				elementStack = append(elementStack, last)
			} else {
				last = nil
				elementStack = append(elementStack, &elementRef{name: name})
			}
			buffer.Reset()
		case xml.EndElement:
			key := ""
			for i, elementRef := range elementStack {
				if i > 0 {
					key += "/"
				}
				key += elementRef.String()
			}
			text := strings.TrimSpace(buffer.String())
			floatVal, _, err := big.ParseFloat(text, 10, 30, big.ToNearestEven)
			if err == nil {
				result[key] = floatVal
			} else {
				result[key] = text
			}
			elementStack, last = elementStack[0:len(elementStack)-1], elementStack[len(elementStack)-1]
		case xml.CharData:
			buffer.Write(token.(xml.CharData))
		}
	}
}
Example #2
0
func ParseFloat(s string, base int, prec uint, mode big.RoundingMode) (*Float, int, error) {
	f, b, err := big.ParseFloat(s, base, prec, mode)
	return (*Float)(f), b, err
}
Example #3
0
func parseFloat(s string) (*big.Float, bool) {
	v, _, err := big.ParseFloat(s, 0, 1000, big.ToNearestEven)
	return v, err == nil
}
Example #4
0
// Parse a big.Float
func parseBig(number string) (*big.Float, error) {
	bits := digits2bits(uint(len(number)))
	f, _, err := big.ParseFloat(number, DefaultBase, bits, big.ToNearestEven)
	return f, err
}