本文整理汇总了Golang中encoding/xml.Decoder.Token方法的典型用法代码示例。如果您正苦于以下问题:Golang Decoder.Token方法的具体用法?Golang Decoder.Token怎么用?Golang Decoder.Token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类encoding/xml.Decoder
的用法示例。
在下文中一共展示了Decoder.Token方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TransformXML
// TransformXML parses the XML tree, traverses it and calls TransformFunc
// on each XML token, writing the output to the writer, resulting in a
// transformed XML tree
func TransformXML(decoder *xml.Decoder, encoder *xml.Encoder, fn TransformFunc) error {
parentNodes := &NodeList{}
for {
token, err := decoder.Token()
if err != nil {
if err != io.EOF {
return trace.Wrap(err)
}
break
}
for _, t := range fn(parentNodes, token) {
if err := encoder.EncodeToken(t); err != nil {
return err
}
}
switch e := token.(type) {
case xml.StartElement:
parentNodes.Push(e)
case xml.EndElement:
parentNodes.Pop()
}
}
encoder.Flush()
return nil
}
示例2: NewTree
func NewTree(dec *xml.Decoder) (Node, error) {
var stack []*Element // stack of elements
var result *Element
for {
tok, err := dec.Token()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
switch tok := tok.(type) {
case xml.StartElement:
newElem := &Element{tok.Name, tok.Attr, []Node{}}
if len(stack) > 0 {
elem := stack[len(stack)-1]
elem.Children = append(elem.Children, newElem)
}
stack = append(stack, newElem)
if result == nil {
result = newElem
}
case xml.EndElement:
stack = stack[:len(stack)-1] // pop
case xml.CharData:
if len(stack) == 0 {
continue
}
elem := stack[len(stack)-1]
elem.Children = append(elem.Children, CharData(tok))
}
}
return result, nil
}
示例3: UnmarshalXML
func (x *XmlNode) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
x.Name = getName(start.Name)
x.Attrs = map[string]string{}
for _, attr := range start.Attr {
x.Attrs[getName(attr.Name)] = attr.Value
}
token, err := d.Token()
for token != start.End() {
if err != nil {
return err
}
next, ok := token.(xml.StartElement)
if ok {
child := XmlNode{}
err = child.UnmarshalXML(d, next)
if err != nil {
return err
}
x.Children = append(x.Children, child)
} else {
text, ok := token.(xml.CharData)
if ok {
x.Children = append(x.Children, TextNode(string([]byte(text))))
}
}
token, err = d.Token()
}
return nil
}
示例4: readOptiongroup
func readOptiongroup(attributes []xml.Attr, dec *xml.Decoder) *Optiongroup {
og := &Optiongroup{}
og.ShortDescription = make(map[string]string)
forloop:
for {
t, err := dec.Token()
if err != nil {
break
}
switch v := t.(type) {
case xml.StartElement:
switch v.Name.Local {
case "shortdescription":
lang, text := readShortDescription(v.Attr, dec)
og.ShortDescription[lang] = text
case "classoption":
og.Classoption = append(og.Classoption, readClassoption(v.Attr, dec))
}
case xml.EndElement:
if v.Name.Local == "optiongroup" {
break forloop
}
}
}
return og
}
示例5: readVariant
func readVariant(attributes []xml.Attr, dec *xml.Decoder) Variant {
variant := Variant{}
variant.Description = make(map[string]template.HTML)
for _, attribute := range attributes {
if attribute.Name.Local == "name" {
variant.Name = attribute.Value
}
}
for {
t, err := dec.Token()
if err != nil {
break
}
switch v := t.(type) {
case xml.StartElement:
switch v.Name.Local {
case "argument":
variant.Arguments = append(variant.Arguments, readArgument(v.Attr, dec))
case "description":
lang, text := readDescription(v.Attr, dec)
variant.Description[lang] = text
}
case xml.EndElement:
if v.Name.Local == "variant" {
return variant
}
}
}
return variant
}
示例6: getStructMember
func getStructMember(parser *xml.Decoder) (member Struct) {
var token xml.Token
token, _ = parser.Token()
member = Struct{}
for {
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "name" {
member["name"], _ = getElementValue(parser)
}
if t.Name.Local == "value" {
member["value"], _ = getValue(parser)
}
case xml.EndElement:
if t.Name.Local == "member" {
return member
}
}
token, _ = parser.Token()
}
return
}
示例7: XMLText
func XMLText(decoder *xml.Decoder) (string, error) {
r := ""
for {
token, err := decoder.Token()
if err == io.EOF {
return r, io.EOF
}
switch token := token.(type) {
case xml.EndElement:
return r, nil
case xml.CharData:
r += string(token)
case xml.StartElement:
sub, err := XMLText(decoder)
r += sub
if err != nil {
return r, err
}
case xml.Comment: // ignore
case xml.ProcInst: // ignore
case xml.Directive: // ignore
default:
panic("unknown token")
}
}
}
示例8: readTaxonLineage
func readTaxonLineage(dec *xml.Decoder, tx *taxon) error {
for tk, err := dec.Token(); ; tk, err = dec.Token() {
if err != nil {
return err
}
switch t := tk.(type) {
case xml.StartElement:
if t.Name.Local == "taxon" {
for _, at := range t.Attr {
switch at.Name.Local {
case "taxId":
tx.par = append(tx.par, at.Value)
case "scientificName":
tx.lin = append(tx.lin, at.Value)
}
}
}
case xml.EndElement:
if t.Name.Local != "taxon" {
return nil
}
}
}
}
示例9: UnmarshalXML
// UnmarshalXML function unmarshal an <album-list> XML fragment to a Map[string]*Album
func (am *AlbumMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// result
m := map[string]*Album{}
LOOP:
for {
token, err := d.Token()
if err != nil {
return err
}
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "album" {
a := Album{}
elmt := xml.StartElement(t)
d.DecodeElement(&a, &elmt)
m[a.ID] = &a
}
case xml.EndElement:
if t.Name.Local == "album-list" {
break LOOP
}
}
}
*am = AlbumMap(m)
return nil
}
示例10: readXmlText
func readXmlText(decoder *xml.Decoder) (string, error) {
var context string
for {
token, err := decoder.Token()
if nil != err {
return context, err
}
switch v := token.(type) {
case xml.EndElement:
return context, nil
case xml.CharData:
context = string(v)
case xml.StartElement:
switch v.Name.Local {
case "Datetime":
txt, e := readXmlText(decoder)
if nil != e {
return "", e
}
if e = exitElement(decoder, 0); nil != e {
return txt, e
}
return txt, nil
default:
return context, errors.New("element '" + v.Name.Local + "' is not excepted, excepted is CharData")
}
default:
return context, fmt.Errorf("token '%T' is not excepted, excepted is CharData", v)
}
}
}
示例11: ReadEnvelopeHeader
func ReadEnvelopeHeader(decoder *xml.Decoder) (string, error) {
var action string
for {
t, err := decoder.Token()
if nil != err {
return "", err
}
switch v := t.(type) {
case xml.EndElement:
return action, nil
case xml.StartElement:
switch v.Name.Local {
case "Action":
action, err = readXmlText(decoder)
if nil != err {
return "", err
}
default:
if e := skipElement(decoder, 0); nil != e {
return "", e
}
}
}
}
}
示例12: UnmarshalXML
func (t *Tag) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
t.Name = start.Name
t.Attr = start.Attr
for {
token, err := d.Token()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
switch token.(type) {
case xml.StartElement:
tok := token.(xml.StartElement)
var data *Tag
if err := d.DecodeElement(&data, &tok); err != nil {
return err
}
t.Children = append(t.Children, data)
case xml.CharData:
t.Children = append(t.Children, token.(xml.CharData).Copy())
case xml.Comment:
t.Children = append(t.Children, token.(xml.Comment).Copy())
}
}
return nil
}
示例13: WalkNodePath
func WalkNodePath(dec *xml.Decoder, selector string) (xml.StartElement, error) {
if selector == "" {
return xml.StartElement{}, errors.New("invalid path")
}
splitfront := func(p string) (front string, tail string) {
i := strings.IndexRune(p, '/')
if i >= 0 {
return p[:i], p[i+1:]
}
return p, ""
}
var nextid string
nextid, selector = splitfront(selector)
for {
token, err := dec.Token()
if err != nil {
return xml.StartElement{}, err
}
start, isStart := token.(xml.StartElement)
if isStart && strings.EqualFold(nextid, getAttr(&start, "id")) {
nextid, selector = splitfront(selector)
if nextid == "" {
return start, nil
}
}
}
panic("unreachable")
}
示例14: getNextToken
func getNextToken(p *xml.Decoder) (*xmlToken, error) {
tag, err := p.Token()
if tag == nil || err != nil {
return nil, err
}
if tokenMap == nil {
initTokenMap()
}
switch v := tag.(type) {
case xml.StartElement:
tok, err := getTagToken(v.Name.Local)
if err != nil {
return nil, err
}
return &xmlToken{token: tok, isStart: true}, nil
case xml.EndElement:
tok, err := getTagToken(v.Name.Local)
if err != nil {
return nil, err
}
return &xmlToken{token: tok, isStart: false}, nil
case xml.CharData:
return &xmlToken{token: tokenText, text: string(v)}, nil
case xml.ProcInst:
return &xmlToken{token: tokenProcInst}, nil
default:
return nil, fmt.Errorf("Not handling XML token %v (type %T)", v, v)
}
}
示例15: nextElement
// Scan XML token stream to finc next Element (start or end)
func nextElement(p *xml.Decoder) (xml.Token, error) {
for {
t, err := p.Token()
if err != nil {
return xml.StartElement{}, err
}
switch elem := t.(type) {
case xml.StartElement, xml.EndElement:
return t, nil
case xml.CharData:
// https://xmpp.org/rfcs/rfc6120.html#xml-whitespace
// rfc6120, section 1.4: "whitespace" is used to refer to any character
// or characters matching [...] SP, HTAB, CR, or LF.
switch string(elem) {
case " ", "\t", "\r", "\n": //TODO: consider more than one whitespace
log.Println("xmpp: received whitespace ping")
}
case xml.ProcInst:
if !(elem.Target == "xml" && strings.HasPrefix(string(elem.Inst), "version=")) {
log.Printf("xmpp: received unhandled ProcInst element: target=%s inst=%s\n", elem.Target, string(elem.Inst))
}
default:
log.Printf("xmpp: received unhandled element: %#v\n", elem)
}
}
}