#coding:utf-8
import os
import os.path
import xml.dom.minidom
import xlrd
import xlwt
def get_xml_dom(xml_file=''):
"""
"""
if xml_file:
dom = xml.dom.minidom.parse(xml_file)
else:
dom = xml.dom.minidom.Document()
#root = dom.documentElement
#name = root.getElementsByTagName('sound')
return dom
def create_node(dom,new_node,parent_node=''):
"""
创建节点(包括根节点)
"""
if not parent_node:
parent_node = dom.createElement('root')
dom.appendChild(parent_node)
node = dom.createElement(new_node)
parent_node.appendChild(node)
return node
def create_node_text(dom,node,txt):
"""
节点赋值
"""
txt = dom.createTextNode(txt)
node.appendChild(txt)
return node
def get_node_value(node,tag):
"""
"""
for x in node:
nm = x.nodeName
print nm
if nm == tag:
return x.firstChild.data
def get_node_data(node):
"""
获得节点文本
"""
return node.firstChild.data
def set_node_data(node,txt):
"""
节点赋值 只有节点上有值 才能 x.firstChild.data
"""
for x in node:
x.firstChild.data = txt
return node
def get_node_name(node):
"""
获得节点名
"""
return node.nodeName
def set_node_attribute(node,key,val):
"""
设置节点属性
"""
node.setAttribute(key,val)
return node
def remove_node_child(parent_node,c_node):
"""
删除子节点
"""
return parent_node.removeChild(c_node)
def get_elementsByTagName(dom,tag):
"""
获取标签
"""
root = dom
return root.getElementsByTagName(tag)
def write_xml(dom='',new_xml_file='new_xml.xml'):
"""
生成xml
"""
if not dom:
dom = get_xml_dom()
#.writexml()第一个参数是目标文件对象,第二个参数是根节点的缩进格式,第三个参数是其他子节点的缩进格式,
# 第四个参数制定了换行格式,第五个参数制定了xml内容的编码。
with open(new_xml_file,'w') as fx:
dom.writexml(fx,indent='',addindent='\t',newl='\n',encoding='UTF-8')
def read_xlsx(filename):
"""
读取excel表格
"""
rdx = xlrd.open_workbook(filename)
sheet = rdx.sheet_by_index(0)
#sheet = data.sheets()[0]
#sheet = data.sheet_by_name(u'sheet1')
#行数
nrows = sheet.nrows
#列数
ncols = sheet.ncols
for r in xrange(0,nrows):
#单元行的值 row_val 为list
row_val = sheet.row_values(x)
#第二行第二列的值
row = 1
col = 1
sheet.cell_value(row,col)
def test():
"""
"""
dom = get_xml_dom()
school = create_node(dom,'school')
create_node(dom,'house',school)
house_node = get_elementsByTagName(dom,'house')
create_node_text(dom,house_node[0],'fuck')
print get_node_data(house_node[0])
set_node_data(house_node,'fff')
write_xml(dom)
"""
fff
"""
test()