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


Golang Element.CreateAttr方法代码示例

本文整理汇总了Golang中github.com/beevik/etree.Element.CreateAttr方法的典型用法代码示例。如果您正苦于以下问题:Golang Element.CreateAttr方法的具体用法?Golang Element.CreateAttr怎么用?Golang Element.CreateAttr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/beevik/etree.Element的用法示例。


在下文中一共展示了Element.CreateAttr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getSortedNamespaces

// getSortedNamespaces sorts the namespace attributes by their prefix
func getSortedNamespaces(list map[string]string) []etree.Attr {
	var keys []string
	for k := range list {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	elem := etree.Element{}
	for _, k := range keys {
		elem.CreateAttr(k, list[k])
	}

	return elem.Attr
}
开发者ID:ma314smith,项目名称:signedxml,代码行数:15,代码来源:exclusivecanonicalization.go

示例2: SetBuilderElementValue

// SetBuilderElementValue if it will change to struct from map if the future's
// author is feeling a bothersome in this function.
func SetBuilderElementValue(elm *etree.Element, data map[string]interface{}, basekey string) (*etree.Element, bool) {
	var child *etree.Element

	key := basekey
	ts, tk := spaceDecompose(elm.Tag)
	_, sk := spaceDecompose(elm.Space)

	if elm.Tag != "" && ts != "" && tk != "" {
		key = fmt.Sprintf("%s:%s", elm.Space, basekey)
	} else if sk != "" {
		key = fmt.Sprintf("%s:%s", sk, basekey)
	}

	if values, ok := data[basekey]; ok {
		switch value := values.(type) {
		case nil:
		default:
			child = elm.CreateElement(key)
			child.SetText(fmt.Sprint(value))
		case int:
			child = elm.CreateElement(key)
			child.SetText(fmt.Sprint(value))
		case string:
			child = elm.CreateElement(key)
			child.SetText(value)
		case float64, float32:
			child = elm.CreateElement(key)
			child.SetText(fmt.Sprint(value))
		case time.Time:
			child = elm.CreateElement(key)
			child.SetText(value.Format(time.RFC3339))
		case bool:
			_ = elm.CreateElement(fmt.Sprintf("%s:%s", key, key))
		case []int:
			for _, v := range value {
				child = elm.CreateElement(key)
				child.SetText(fmt.Sprint(v))
			}
		case []string:
			for _, v := range value {
				child = elm.CreateElement(key)
				child.SetText(v)
			}
		case Attrs:
			val, attrs := value[0], value[1]

			child, _ = SetBuilderElementValue(elm, URL{basekey: val}, basekey)
			switch attr := attrs.(type) {
			case map[string]string:
				for k, v := range attr {
					child.CreateAttr(k, v)
				}
			// TODO: gotta remove below
			case Attr:
				for k, v := range attr {
					child.CreateAttr(k, v)
				}
			}

		case interface{}:
			var childkey string
			if sk == "" {
				childkey = fmt.Sprintf("%s:%s", key, key)
			} else {
				childkey = fmt.Sprint(key)
			}

			switch value := values.(type) {
			case []URL:
				for _, v := range value {
					child := elm.CreateElement(childkey)
					for ck := range v {
						SetBuilderElementValue(child, v, ck)
					}
				}
			case URL:
				child := elm.CreateElement(childkey)
				for ck := range value {
					SetBuilderElementValue(child, value, ck)
				}
			}
		}

		return child, true
	}

	return child, false
}
开发者ID:ikeikeikeike,项目名称:go-sitemap-generator,代码行数:90,代码来源:utils.go


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