• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang xml.CharData函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中encoding/xml.CharData函数的典型用法代码示例。如果您正苦于以下问题:Golang CharData函数的具体用法?Golang CharData怎么用?Golang CharData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: getTokenVariations

//This function creates variations on tokens without regards as to positions in the file.
func getTokenVariations(t xml.Token) []xml.Token {
	var result []xml.Token = make([]xml.Token, 0)
	switch t := t.(type) {
	case xml.CharData:
		{
			//If the token is a number try some random number
			if _, err := strconv.Atoi(string(t)); err == nil {
				result = append(result, xml.CharData(randInt(rand.Intn(15))))
			}

			result = append(result, xml.CharData(randString(rand.Intn(100))))
			return result
		}
	case xml.StartElement:
		{
			for k := range t.Attr {
				if _, err := strconv.Atoi(string(t.Attr[k].Value)); err == nil {
					start := xml.CopyToken(t).(xml.StartElement)
					start.Attr[k].Value = string(randInt(rand.Intn(15)))
					result = append(result, start)
				}
				start := xml.CopyToken(t).(xml.StartElement)
				start.Attr[k].Value = string(randString(rand.Intn(100)))
				result = append(result, start)
			}
			return result
		}

	default:
		{
			return make([]xml.Token, 0) // No variations on non char tokens yet
		}
	}
}
开发者ID:Richardphp,项目名称:noms,代码行数:35,代码来源:fuzzy_test.go


示例2: swCustomized

func swCustomized() {
	//println("\nswCustomized")
	d := xml.NewDecoder(strings.NewReader(flatxml))
	e := xml.NewEncoder(os.Stdout)

	nesting := -1
	for {
		t, err := d.Token()
		if err == io.EOF {
			e.Flush()
			return
		}
		if err != nil {
			log.Fatal(err)
		}
		if _, ok := t.(xml.StartElement); ok {
			nesting++
		}
		if nesting > 0 {
			e.EncodeToken(xml.CharData(bytes.Repeat([]byte("  "), nesting)))
		}
		e.EncodeToken(t)
		e.EncodeToken(xml.CharData([]byte{'\n'}))
		if _, ok := t.(xml.EndElement); ok {
			nesting--
		}
		e.Flush()
	}
}
开发者ID:suntong,项目名称:lang,代码行数:29,代码来源:xml-beautify-string.go


示例3: marshalDescription

func marshalDescription(eltname string, e *xml.Encoder, desc map[string]template.HTML) error {
	var err error
	for lang, text := range desc {
		startElt := xml.StartElement{Name: xml.Name{Local: eltname}}

		startElt.Attr = []xml.Attr{
			xml.Attr{Name: xml.Name{Local: "lang"}, Value: lang},
		}

		err = e.EncodeToken(startElt)
		if err != nil {
			return err
		}

		err = e.EncodeToken(xml.CharData(string(text)))
		if err != nil {
			return err
		}

		err = e.EncodeToken(xml.EndElement{Name: startElt.Name})
		if err != nil {
			return err
		}

	}
	return nil
}
开发者ID:speedata,项目名称:ltxdoc,代码行数:27,代码来源:xmlout.go


示例4: StructToXML

// StructToXML writes an XMLNode to a xml.Encoder as tokens.
func StructToXML(e *xml.Encoder, node *XMLNode, sorted bool) error {
	e.EncodeToken(xml.StartElement{Name: node.Name, Attr: node.Attr})

	if node.Text != "" {
		e.EncodeToken(xml.CharData([]byte(node.Text)))
	} else if sorted {
		sortedNames := []string{}
		for k := range node.Children {
			sortedNames = append(sortedNames, k)
		}
		sort.Strings(sortedNames)

		for _, k := range sortedNames {
			for _, v := range node.Children[k] {
				StructToXML(e, v, sorted)
			}
		}
	} else {
		for _, c := range node.Children {
			for _, v := range c {
				StructToXML(e, v, sorted)
			}
		}
	}

	e.EncodeToken(xml.EndElement{Name: node.Name})
	return e.Flush()
}
开发者ID:40a,项目名称:bootkube,代码行数:29,代码来源:xml_to_struct.go


示例5: TrimSpace

// TrimSpace is a transformer function that replaces CDATA with blank
// characters with empty strings
func TrimSpace(parents *NodeList, in xml.Token) []xml.Token {
	switch t := in.(type) {
	case xml.CharData:
		return []xml.Token{xml.CharData(bytes.TrimSpace(t))}
	}
	return []xml.Token{in}
}
开发者ID:gravitational,项目名称:configure,代码行数:9,代码来源:xml.go


示例6: MarshalXML

// MarshalXML ensures addresses marshal to nil if empty without the need
// to use pointers.
func (a Address) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if a.Address == "" && a.Address2 == "" && a.City == "" && a.State == "" && a.Zip == "" && a.Country == "" && a.Phone == "" {
		return nil
	}

	e.EncodeToken(xml.StartElement{Name: xml.Name{Local: "address"}})
	if a.Address != "" {
		s := xml.StartElement{Name: xml.Name{Local: "address1"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.Address)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.Address2 != "" {
		s := xml.StartElement{Name: xml.Name{Local: "address2"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.Address2)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.City != "" {
		s := xml.StartElement{Name: xml.Name{Local: "city"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.City)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.State != "" {
		s := xml.StartElement{Name: xml.Name{Local: "state"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.State)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.Zip != "" {
		s := xml.StartElement{Name: xml.Name{Local: "zip"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.Zip)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.Country != "" {
		s := xml.StartElement{Name: xml.Name{Local: "country"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.Country)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	if a.Phone != "" {
		s := xml.StartElement{Name: xml.Name{Local: "phone"}}
		e.EncodeToken(s)
		e.EncodeToken(xml.CharData([]byte(a.Phone)))
		e.EncodeToken(xml.EndElement{Name: s.Name})
	}

	e.EncodeToken(xml.EndElement{Name: xml.Name{Local: "address"}})

	return nil
}
开发者ID:fubotv,项目名称:go-recurly,代码行数:61,代码来源:accounts.go


示例7: parseXMLtoTOML

// parseXMLtoTOML parses XML to TOML (in a slightly brain dead way).
func parseXMLtoTOML(input []byte) {
	parser := xml.NewDecoder(bytes.NewReader(input))
	keywords := []string{}
	name := ""
	fmt.Println("% # Quick 'n dirty translated by mmark")
	for {
		token, err := parser.Token()
		if err != nil {
			break
		}
		switch t := token.(type) {
		case xml.StartElement:
			elmt := xml.StartElement(t)
			name = elmt.Name.Local
			switch name {
			case "author":
				fmt.Println("%\n% [[author]]")
				outAttr(elmt.Attr)
			case "rfc":
				fallthrough
			case "title":
				outAttr(elmt.Attr)
			case "address":
				fmt.Println("% [author.address]")
			case "postal":
				fmt.Println("% [author.address.postal]")
			case "date":
				outDate(elmt.Attr)
			}
		case xml.CharData:
			if name == "" {
				continue
			}
			data := xml.CharData(t)
			data = bytes.TrimSpace(data)
			if len(data) == 0 {
				continue
			}
			if name == "keyword" {
				keywords = append(keywords, "\""+string(data)+"\"")
				continue
			}
			outString(name, string(data))
		case xml.EndElement:
			name = ""
		case xml.Comment:
			// don't care
		case xml.ProcInst:
			// don't care
		case xml.Directive:
			// don't care
		default:
		}
	}
	outArray("keyword", keywords)
}
开发者ID:paulej,项目名称:mmark,代码行数:57,代码来源:titleblock.go


示例8: parseProfileXML

func parseProfileXML(profileName string, text string) Profile {
	p := new(Profile)
	p.name = profileName
	p.fieldPermissions = map[string]FLS{}
	p.objectPermissions = map[string]OLS{}
	var currentElement XLS

	r := strings.NewReader(text)
	parser := xml.NewDecoder(r)
	depth := 0

	eltType := ""
	propertyName := ""

	for {

		token, err := parser.Token()
		if err != nil {
			break
		}
		switch t := token.(type) {
		case xml.StartElement:
			elmt := xml.StartElement(t)
			name := elmt.Name.Local
			if depth == 1 {
				eltType = name
				if eltType == "objectPermissions" {
					currentElement = new(OLS)
				} else if eltType == "fieldPermissions" {
					currentElement = new(FLS)
				} else {
					currentElement = nil
				}
			}
			if depth == 2 {
				propertyName = name
			}
			depth++
		case xml.EndElement:
			if depth == 2 && currentElement != nil {
				currentElement.addToProfile(*p)
			}
			depth--
		case xml.CharData:
			bytes := xml.CharData(t)
			if currentElement != nil && depth == 3 {
				currentElement.addProperty(propertyName, string(bytes))
			}
		default:
		}
	}

	//	fmt.Println(p)

	return *p
}
开发者ID:ChaitanyaRanade,项目名称:force,代码行数:56,代码来源:security.go


示例9: DumpString

func (d *XmlDataOutput) DumpString(field, value string) {
	if d.lastError != nil {
		return
	}
	d.lastError = d.writeTokens([]xml.Token{
		xml.StartElement{Name: xml.Name{Local: field}},
		xml.CharData(value),
		xml.EndElement{Name: xml.Name{Local: field}},
	})
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:10,代码来源:xml_data_output.go


示例10: getChildElements

func getChildElements(commands *commandsxml.CommandsXML, enc *xml.Encoder, children []byte, lang string) {
	if len(children) == 0 {
		enc.EncodeToken(emptyElement.Copy())
		enc.EncodeToken(emptyElement.End())
	}
	buf := bytes.NewBuffer(children)
	dec := xml.NewDecoder(buf)
	for {
		tok, err := dec.Token()
		if err != nil {
			return
		}
		switch v := tok.(type) {
		case xml.StartElement:
			switch v.Name.Local {
			case "cmd":
				ref := refElement.Copy()
				for _, attr := range v.Attr {
					if attr.Name.Local == "name" {
						ref.Attr = []xml.Attr{{Name: xml.Name{Local: "name"}, Value: "e_" + attr.Value}}
					}
				}
				enc.EncodeToken(ref)
			case "description":
			case "choice":
				enc.EncodeToken(choiceElement.Copy())
				for _, attribute := range v.Attr {
					if attribute.Name.Local == lang {
						enc.EncodeToken(valueElement.Copy())
						enc.EncodeToken(xml.CharData(attribute.Value))
						enc.EncodeToken(valueElement.End())
					}
				}
			case "reference":
				for _, attr := range v.Attr {
					if attr.Name.Local == "name" {
						getChildElements(commands, enc, commands.GetDefine(attr.Value), lang)
					}
				}
			default:
				enc.EncodeToken(v.Copy())
			}

		case xml.EndElement:
			switch v.Name.Local {
			case "cmd":
				enc.EncodeToken(refElement.End())
			case "choice":
				enc.EncodeToken(choiceElement.End())
			default:
				enc.EncodeToken(v)
			}
		}
	}
}
开发者ID:speedata,项目名称:publisher,代码行数:55,代码来源:genschema.go


示例11: DumpDouble

func (d *XmlDataOutput) DumpDouble(field string, value float64) {
	if d.lastError != nil {
		return
	}
	if err := d.writeTokens([]xml.Token{
		xml.StartElement{Name: xml.Name{Local: field}},
		xml.CharData(strconv.FormatFloat(value, 'e', 10, 64)),
		xml.EndElement{Name: xml.Name{Local: field}},
	}); err != nil {
		d.lastError = errors.Wrap(err, 0)
	}
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:12,代码来源:xml_data_output.go


示例12: DumpUnsignedLong

func (d *XmlDataOutput) DumpUnsignedLong(field string, value uint64) {
	if d.lastError != nil {
		return
	}
	if err := d.writeTokens([]xml.Token{
		xml.StartElement{Name: xml.Name{Local: field}},
		xml.CharData(strconv.FormatUint(value, 10)),
		xml.EndElement{Name: xml.Name{Local: field}},
	}); err != nil {
		d.lastError = errors.Wrap(err, 0)
	}
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:12,代码来源:xml_data_output.go


示例13: getCharData

func getCharData(p parser.Interface) xml.CharData {
	i, err := p.Int()
	if err == nil {
		return xml.CharData([]byte(strconv.FormatInt(i, 10)))
	}
	u, err := p.Uint()
	if err == nil {
		return xml.CharData([]byte(strconv.FormatUint(u, 10)))
	}
	d, err := p.Double()
	if err == nil {
		return xml.CharData([]byte(strconv.FormatFloat(d, 'e', -1, 64)))
	}
	b, err := p.Bool()
	if err == nil {
		return xml.CharData([]byte(strconv.FormatBool(b)))
	}
	s, err := p.String()
	if err == nil {
		return xml.CharData([]byte(s))
	}
	v, err := p.Bytes()
	if err == nil {
		return xml.CharData([]byte(base64.StdEncoding.EncodeToString(v)))
	}
	return nil
}
开发者ID:katydid,项目名称:katydid,代码行数:27,代码来源:xml.go


示例14: DumpTime

func (d *XmlDataOutput) DumpTime(field string, value time.Time) {
	if d.lastError != nil {
		return
	}
	if err := d.writeTokens([]xml.Token{
		xml.StartElement{Name: xml.Name{Local: field}},
		xml.CharData(strconv.FormatInt(value.Unix(), 10)),
		xml.EndElement{Name: xml.Name{Local: field}},
		xml.Comment(value.String()),
	}); err != nil {
		d.lastError = errors.Wrap(err, 0)
	}
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:13,代码来源:xml_data_output.go


示例15: main

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage: ", os.Args[0], "file")
		os.Exit(1)
	}
	file := os.Args[1]
	bytes, err := ioutil.ReadFile(file)
	checkError(err)
	r := strings.NewReader(string(bytes))

	parser := xml.NewDecoder(r)
	depth := 0
	for {
		token, err := parser.Token()
		if err != nil {
			break
		}
		switch t := token.(type) {
		case xml.StartElement:
			elmt := xml.StartElement(t)
			name := elmt.Name.Local
			printElmt(name, depth)
			depth++
		case xml.EndElement:
			depth--
			elmt := xml.EndElement(t)
			name := elmt.Name.Local
			printElmt(name, depth)
		case xml.CharData:
			bytes :=
				xml.CharData(t)
			printElmt("\""+string([]byte(bytes))+"\"",
				depth)
		case
			xml.Comment:
			printElmt("Comment",
				depth)
		case
			xml.ProcInst:
			printElmt("ProcInst",
				depth)
		case
			xml.Directive:
			printElmt("Directive",
				depth)
		default:
			fmt.Println("Unknown")
		}
	}
}
开发者ID:sjtlqy,项目名称:Go,代码行数:50,代码来源:ParseXML.go


示例16: Elementize

func Elementize(dec *xml.Decoder) (data string) {
	var parentNode string
	var attrs bool
	for {
		token, err := dec.Token()
		if err != nil {
			break
		}

		switch t := token.(type) {
		case xml.StartElement:
			parentNode = t.Name.Local
			data += fmt.Sprintf("<%s>", t.Name.Local)
			if len(t.Attr) > 0 {
				attrs = true
				// data += "<attr>"
				for _, v := range t.Attr {
					data += fmt.Sprintf("<%s>%s</%s>", v.Name.Local, v.Value, v.Name.Local)
				}
				// data += "</attr>"
			} else {
				attrs = false
			}

		case xml.EndElement:
			data += fmt.Sprintf("</%s>", t.Name.Local)

		case xml.CharData:
			val := strings.Replace(strings.TrimSpace(string(xml.CharData(t))), "\n", "", -1)
			if val != "" {
				// data += fmt.Sprintf("<value>%s</value>", val)
				if attrs {
					data += fmt.Sprintf("<%s>%s</%s>", parentNode, val, parentNode)

				} else {
					data += fmt.Sprintf("%s", val)

				}
			}

		case xml.ProcInst:
			// fmt.Printf("not supported: %v\n", t)

		default:
			fmt.Printf("type not supported: %v\n", t)
		}
	}
	return
}
开发者ID:brydavis,项目名称:xql,代码行数:49,代码来源:main.go


示例17: NewCompactSearchResult

// NewCompactSearchResult _always_ close this
func NewCompactSearchResult(body io.ReadCloser) (*CompactSearchResult, error) {
	parser := DefaultXMLDecoder(body, false)
	result := &CompactSearchResult{
		body:   body,
		parser: parser,
	}
	// extract the basic content before delving into the data
	for {
		token, err := parser.Token()
		if err != nil {
			return result, err
		}
		switch t := token.(type) {
		case xml.StartElement:
			// clear any accumulated data
			result.buf.Reset()
			switch t.Name.Local {
			case XMLElemRETS, XMLElemRETSStatus:
				resp, er := ResponseTag(t).Parse()
				if er != nil {
					return result, er
				}
				result.Response = *resp
			case "COUNT":
				result.Count, err = countTag(t).Parse()
				if err != nil {
					return result, err
				}
			case "DELIMITER":
				result.Delimiter, err = DelimiterTag(t).Parse()
				if err != nil {
					return result, err
				}
			}
		case xml.EndElement:
			switch t.Name.Local {
			case "COLUMNS":
				result.Columns = CompactRow(result.buf.String()).Parse(result.Delimiter)
				return result, nil
			case XMLElemRETS, XMLElemRETSStatus:
				// if there is only a RETS tag.. just exit
				return result, nil
			}
		case xml.CharData:
			bytes := xml.CharData(t)
			result.buf.Write(bytes)
		}
	}
}
开发者ID:jpfielding,项目名称:gorets,代码行数:50,代码来源:search_compact.go


示例18: format

func format(encoder *xml.Encoder, decoder *xml.Decoder, d core.Dict) (nilMap map[string]int) {

	// nilArr  := make([]string, 10)
	// nilCnt  := 0
	nilMap = make(map[string]int)
	dict := d.GetData()

	focus := false
	for tk, e := decoder.Token(); e == nil; tk, e = decoder.Token() {

	S:
		switch tmp := tk.(type) {

		case xml.StartElement:
			focus = tmp.Name.Local == "string"
			// fmt.Printf("token show : %v\n", tmp.Name.Space)

		// case xml.EndElement:
		case xml.CharData:
			if !focus {
				break S
			}

			content := string([]byte(tmp))
			if strings.IndexFunc(content, matchF) == -1 {
				break S
			}

			if value, ok := dict[content]; ok {

				tk = xml.CharData([]byte(value.ToString()))
			} else {

				if _, ok := nilMap[content]; !ok {

					nilMap[content] = 0
				}
			}

		default:
		}
		encoder.EncodeToken(tk)
	}

	return
}
开发者ID:hiank,项目名称:translate,代码行数:46,代码来源:trans-ccb.go


示例19: set

func set(n *node, s string) (string, error) {
	if n == nil {
		return s, nil
	}

	switch n.typ {
	case BARE:
		return set(n.pre, s)
	case BASE64:
		return set(n.pre, base64.StdEncoding.EncodeToString([]byte(s)))
	case BASE64_URL:
		return set(n.pre, base64.URLEncoding.EncodeToString([]byte(s)))
	case URL_ENCODE:
		return set(n.pre, url.QueryEscape(s))
	case JSONMAP:
		obj, _ := n.obj.(*jsonMap)
		save := obj.m[obj.k]
		obj.m[obj.k] = s
		j, err := json.Marshal(obj.org)
		obj.m[obj.k] = save
		if err != nil {
			return "", err
		}
		return set(n.pre, string(j))
	case JSONARRAY:
		obj, _ := n.obj.(*jsonArray)
		save := obj.a[obj.i]
		obj.a[obj.i] = s
		j, err := json.Marshal(obj.org)
		obj.a[obj.i] = save
		if err != nil {
			return "", err
		}
		return set(n.pre, string(j))
	case XMLOBJ:
		obj, _ := n.obj.(*xmlObj)
		save := obj.xt[obj.index].token
		obj.xt[obj.index].token = xml.CharData(s)
		x := obj.marshal()
		obj.xt[obj.index].token = save
		return set(n.pre, string(x))
	default:
		panic("Decode type invalid!")
	}
}
开发者ID:postfix,项目名称:ivega,代码行数:45,代码来源:decode.go


示例20: Decode

// Decode reads the next JSON-encoded value from its
// input and stores it in the value pointed to by v.
func (dec *Decoder) Decode(root *Node) error {
	xmlDec := xml.NewDecoder(dec.r)

	// Create first element from the root node
	elem := &element{
		parent: nil,
		n:      root,
	}

	for {
		t, _ := xmlDec.Token()
		if t == nil {
			break
		}

		switch se := t.(type) {
		case xml.StartElement:
			// Build new a new current element and link it to its parent
			elem = &element{
				parent: elem,
				n:      &Node{},
				label:  se.Name.Local,
			}

			// Extract attributes as children
			for _, a := range se.Attr {
				elem.n.AddChild(dec.attrPrefix+a.Name.Local, &Node{Data: a.Value})
			}
		case xml.CharData:
			// Extract XML data (if any)
			elem.n.Data = string(xml.CharData(se))
		case xml.EndElement:
			// And add it to its parent list
			if elem.parent != nil {
				elem.parent.n.AddChild(elem.label, elem.n)
			}

			// Then change the current element to its parent
			elem = elem.parent
		}
	}

	return nil
}
开发者ID:DirectX,项目名称:goxml2json,代码行数:46,代码来源:decoder.go



注:本文中的encoding/xml.CharData函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang xml.Escape函数代码示例发布时间:2022-05-24
下一篇:
Golang proto.CompactTextString函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap