AppXml.py
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
"""
XML操作封装
"""
import os.path;
import logging;
import xml.etree.ElementTree as ElementTree;
class XmlNodeValue(object):
STRING = 1;
INT = 2;
FLOAT = 3;
BOOL = 4;
class XmlNodeMap(object):
ATTR = 1;
TEXT = 2;
NODE = 3;
class XmlNode(object):
def __init__(self,currentNode=None,rootNode=None):
self.currentNode = currentNode;
self.rootNode = rootNode;
# 加载XML文件
def LoadFile(self,path):
if os.path.isabs(path): path = os.path.abspath(path);
flag = False;
try:
self.rootNode = ElementTree.parse(path);
if self.rootNode is not None: flag = True;
self.currentNode = self.rootNode;
except Exception,e:
logging.error("XML文件加载失败");
logging.error(e.__str__());
return flag;
# 加载XML内容
def LoadString(self,data):
if data is None or len(data.strip())==0: return False;
flag = False;
try:
self.rootNode = ElementTree.fromstring(data);
if self.rootNode is not None: flag = True;
self.currentNode = self.rootNode;
except Exception,e:
logging.error("XML内容加载失败");
logging.error(e.__str__());
return flag;
# 检查数据是否载入正确
def IsLoad(self):
return self.currentNode is not None and self.rootNode is not None;
# 返回根节点对象
def GetRoot(self):
return XmlNode(self.rootNode,self.rootNode);










