Python動的xmlファイル生成
1、類Person
2、Personによるxmlファイルの動的生成
# -*- coding:UTF-8 -*-
class Person(object):
'''
: , (0: ,1: ),
'''
name = None
sex = None
mobile = None
def __init__(self,name,sex,mobile):
self.name = name
self.sex = sex
self.mobile = mobile
def setName(self,name):
self.name = name
def setSex(self,sex):
self.sex = sex
def setMobile(self,mobile):
self.mobile = mobile
if __name__ == '__main__':
# atts = for attr in if() dir(Person)[0]
pass
2、Personによるxmlファイルの動的生成
def personToXML(person):
''' Person xml '''
name = person.name
sex = person.sex
mobile = person.mobile
root = ET.Element('personList')
personE = ET.SubElement(root, 'person')
nameE = ET.SubElement(personE, 'name')
nameE.text = name
sexE = ET.SubElement(personE, 'sex')
if sex == '0':
sexE.text = ' '
elif sex == '1':
sexE.text = ' '
mobileE = ET.SubElement(personE, 'mobile')
mobileE.text = mobile
rough_string = ET.tostring(root, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" " , encoding="utf-8");