示例#1
0
//保存所有设备配置
func (p *ConfigProtocol) SaveAllDevConfig(r *http.Request, arg *JsonConfigInputAgr, res *JsonResponse) error {
	utils.Debug("received ConfigProtocol SaveAllDevConfig request")
	var (
		n  dom.Node
		t  dom.Element
		dt dom.Attribute
	)
	db := dom.NewDOMBuilder(strings.NewReader(arg.Content), dom.NewDOMStore())
	d, err2 := db.Build()
	if err2 != nil {
		res.Code = 1
		res.ErrMsg = fmt.Sprintf("parse xml file error,category :[%+v] ", err2.Error())
		utils.Error("parse xml file error,category:[%+v]", err2.Error())
		return nil
	}

	f := d.ChildNodes()

	for i := 0; i < len(f); i++ {
		n = f[i]
		switch n.Kind() {
		case dom.ElementKind:
			t = n.(dom.Element)
			switch t.Name() {
			case "Device": // 解析设置
				var v []dom.Attribute
				v = t.Attr()
				for j := 0; j < len(v); j++ {
					dt = v[j]
					if strings.EqualFold(dt.Name(), "Category") {
						err := SaveDrvConfig(dt.Value(), t.String())

						if err != nil {
							res.Code = 1
							res.ErrMsg = fmt.Sprintf("save file error,category :%s  content is : [%s]", dt.Value(), t.String())
							utils.Error("can't save category [%s] ,content is : [%s]", dt.Value(), t.String())
							return nil
						}
					}
				}
				break

			default:
				utils.Error("can't recognize element name: %s", t.Name())
			}
			break
		}
	}

	res.Code = 0

	utils.Debug("end ConfigProtocol SaveAllDevConfig request")
	return nil
}
示例#2
0
func ParserPrintXml(s string) (*PrintFacade, error) {
	utils.Debug("begin parse pr2 string")
	db := dom.NewDOMBuilder(strings.NewReader(s), dom.NewDOMStore())
	d, err := db.Build()
	if err != nil {
		utils.Error("parse xml error %s", err.Error())
		return nil, err
	}
	tmp := parsePrintFormat(d.Root())
	return tmp, nil
}