本文整理汇总了Golang中encoding/xml.CharData函数的典型用法代码示例。如果您正苦于以下问题:Golang CharData函数的具体用法?Golang CharData怎么用?Golang CharData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CharData函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
}
}
示例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()
}
}
示例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
}
示例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()
}
示例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}
}
示例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
}
示例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)
}
示例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
}
示例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}},
})
}
示例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)
}
}
}
}
示例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)
}
}
示例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)
}
}
示例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
}
示例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)
}
}
示例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")
}
}
}