Пример #1
0
Файл: dyn.go Проект: kpmy/xippo
func NewMessage(typ entity.MessageType, to string, body string) (ret dom.Element) {
	ret = dom.Elem(MESSAGE)
	ret.Attr(TYPE, string(typ))
	ret.Attr(ID, strconv.FormatInt(int64(rand.Intn(0xffffff)), 16))
	ret.Attr(TO, to)
	b := dom.Elem(BODY)
	b.AppendChild(dom.Txt(body))
	ret.AppendChild(b)
	return
}
Пример #2
0
func (x *domm) Unmarshal() (err error) {
	d := xml.NewDecoder(x.buf)
	var _t xml.Token
	var this dom.Element
	for stop := false; !stop && err == nil; {
		if _t, err = d.RawToken(); err == nil {
			switch t := _t.(type) {
			case xml.StartElement:
				el := dom.Elem(dom.ThisName(t.Name))
				if x.model == nil {
					x.model = el
					this = el
				} else {
					this.AppendChild(el)
					this = el
				}
				for _, a := range t.Attr {
					this.Attr(dom.ThisName(a.Name), a.Value)
				}
			case xml.CharData:
				if this != nil {
					this.AppendChild(dom.Txt(string(t)))
				} else {
					stop = true
				}
			case xml.EndElement:
				if this != nil {
					if p := this.Parent(); p != nil {
						this = p.(dom.Element)
					} else {
						stop = true
					}
				} else {
					stop = true
				}
			case nil:
			default:
				halt.As(100, reflect.TypeOf(t))
			}
		}
	}
	return
}