當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Element.CreateElement方法代碼示例

本文整理匯總了Golang中github.com/beevik/etree.Element.CreateElement方法的典型用法代碼示例。如果您正苦於以下問題:Golang Element.CreateElement方法的具體用法?Golang Element.CreateElement怎麽用?Golang Element.CreateElement使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/beevik/etree.Element的用法示例。


在下文中一共展示了Element.CreateElement方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: CreateXml

func (aut Person) CreateXml(e *etree.Element) *etree.Element {
	a := e.CreateElement("author")
	a_ln := a.CreateElement("last-name")
	a_mn := a.CreateElement("middle-name")
	a_fn := a.CreateElement("first-name")
	a_ln.SetText(aut.Lname)
	a_mn.SetText(aut.Mname)
	a_fn.SetText(aut.Fname)
	return a
}
開發者ID:ppoloskov,項目名稱:renamefb2books,代碼行數:10,代碼來源:readxml.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

示例3: 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) bool {
	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 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 true
	}
	return false
}
開發者ID:Staylett,項目名稱:go-sitemap-generator,代碼行數:71,代碼來源:utils.go


注:本文中的github.com/beevik/etree.Element.CreateElement方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。