Beispiel #1
0
func main() {
	var docs = []string{doc1, doc2}

	for n, doc := range docs {
		fmt.Println("\nTestValuesFromTagPath2(), iteration:", n, "\n", doc)

		m, _ := x2j.DocToMap(doc)
		fmt.Println("map:", x2j.WriteMap(m))

		v, _ := x2j.ValuesFromTagPath(doc, "data.*")
		fmt.Println("\npath == data.*: len(v):", len(v))
		for key, val := range v {
			fmt.Println(key, ":", val)
		}
		mm := v[0]
		for key, val := range mm.(map[string]interface{}) {
			fmt.Println(key, ":", val)
		}

		v, _ = x2j.ValuesFromTagPath(doc, "data.*.*")
		fmt.Println("\npath == data.*.*: len(v):", len(v))
		for key, val := range v {
			fmt.Println(key, ":", val)
		}
	}
}
Beispiel #2
0
// unmarshal generates an object from the specified XML bytes.
func unmarshal(data string, options objects.Map) (interface{}, error) {

	m, err := xml.DocToMap(data)

	if err != nil {
		return nil, err
	}

	if object, ok := m[XMLObjectElementName]; ok {
		return resolveValues(object), nil
	} else if objects, ok := m[XMLObjectsElementName]; ok {
		return resolveValues(objects.(map[string]interface{})[XMLObjectElementName]), nil
	}

	return nil, nil

}
Beispiel #3
0
func TestFromXml(*testing.T) {
	str := `
<xml><ToUserName><![CDATA[gh_2dc74cccf555]]></ToUserName>
<FromUserName><![CDATA[oSmHgjkiNii6XnhVXVN5Rj5DDARE]]></FromUserName>
<CreateTime>1364877454</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[  uhhgggg]]></Content>
<MsgId>5862104027977744402</MsgId>
</xml>
	`
	root, err := x2j.DocToMap(str)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(Message(root["xml"].(map[string]interface{})).MsgType())
	fmt.Println(Message(root["xml"].(map[string]interface{})).CreateTime())
}
Beispiel #4
0
func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
	file, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	x := &XMLConfigContainer{
		data: make(map[string]interface{}),
	}
	content, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}
	d, err := x2j.DocToMap(string(content))
	if err != nil {
		return nil, err
	}
	x.data = d["config"].(map[string]interface{})
	return x, nil
}
Beispiel #5
0
func (wx *WxHttpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	var msgType string
	var ok bool
	if _Debug {
		start := time.Now()
		defer func() {
			if ok {
				log.Printf("OK : %s , %ds", msgType, time.Now().Unix()-start.Unix())
			} else {
				log.Printf("ERR: %v", req)
			}
		}()
	}
	if err := req.ParseForm(); err != nil {
		log.Println("Bad Req", req)
		rw.WriteHeader(500)
		return
	}
	if !Verify(wx.Token, req.FormValue("timestamp"), req.FormValue("nonce"), req.FormValue("signature")) {
		rw.WriteHeader(403)
		return
	}
	if req.Method == "GET" {
		rw.Write([]byte(req.FormValue("echostr")))
		return
	}

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Println("Req Body Read ERR", req, err)
		rw.WriteHeader(500)
		return
	}
	if DevMode {
		log.Println("Req\n" + string(data))
	}

	root, err := x2j.DocToMap(string(data))
	if err != nil {
		fmt.Println("Bad XML Req", err)
		return
	}
	msg := Message(root["xml"].(map[string]interface{}))
	msgType = msg.MsgType()
	if _Debug {
		log.Println("MsgType =", msgType)
	}
	var reply Replay
	switch msgType {
	case TEXT:
		reply = wx.Handler.Text(msg)
	case IMAGE:
		reply = wx.Handler.Image(msg)
	case LOCATION:
		reply = wx.Handler.Location(msg)
	case LINK:
		reply = wx.Handler.Link(msg)
	case EVENT:
		reply = wx.Handler.Event(msg)
	case VOICE:
		reply = wx.Handler.Voice(msg)
	default:
		reply = wx.Handler.Default(msg)
	}
	if reply == nil {
		ok = true
		if _Debug {
			log.Println("Reply nil")
		}
		return // http 200
	}

	// auto-fix
	if reply.FromUserName() == "" {
		reply.SetFromUserName(msg.ToUserName())
	}
	if reply.ToUserName() == "" {
		reply.SetToUserName(msg.FromUserName())
	}
	if reply.MsgType() == "" {
		reply.SetMsgType(TEXT)
	}
	reply.SetCreateTime(time.Now().Unix())

	if _, ok = reply["FuncFlag"]; !ok {
		reply.SetFuncFlag(0)
	}

	rw.Write([]byte("<xml>"))
	_re := MapToXmlString(reply)
	if _Debug {
		log.Println("Reply\n" + _re)
	}
	rw.Write([]byte(_re))
	rw.Write([]byte("</xml>"))
	ok = true
}