def createEmptyMap(publication_id, model_id): root = ET.element('xml') publication = ET.SubElement(root, 'publication', attrib={'id': publication_id}) model = ET.SubElement(publication, 'model', attrib={'id': model_id}) tree = ET.ElementTree(root) return tree
def draw_line(self, point0, point1, color=None): dist = len(point0 - point1) if dist < 2: return attributes = { 'x1': point0.x, 'y1': point0.y, 'x2': point1.x, 'y2': point1.y, 'stroke': self.to_svg_color(color, 'red'), 'stroke-width': 1 } line = ET.element('line', attrib=attributes) self.output_buffer.get('polygons').append(line)
et.write('test.xml',encoding='utf-8',xml_declaration=True) ################################################################### tree = ET.parse('xml_sample') root = tree.getroot() #创建节点 node = root.makeelement('tagname',{'attr':'value'}) #创建方法2:用类的方法生成(工厂函数) node = ET.Element('name',{"attr":"value"}) #在很节点下添加子节点 root.append(node) #写入文件,并避免自闭合,加上自动生成的注释 tree.write('xml_sample',short_empty_elements=False,xml_declaration=True) #创建ElementTree对象的两个方法 tree = ET.parse(文件路径) tree = ET._ElementTree(根节点(Element对象)) ET.element(标签名,属性) #创建一个element对象,一个element对象就是一个节点 root.append(root.makeelement()#同样在root下创建element对象 ET.SubElement(root,"name",{}))#同样在root下创建Element对象 #要写入中文,加上encoding选项 tree.write('sample',encoding='utf-8') ##################################################### def prettify(root): '''将节点转为字符串,并添加缩进''' rough_str = ET.tostring(root,'utf-8') #minidom模块中的方法,将字符串面为midom中的一个类实例 reparsed = minidom.parseString(rough_string) #使用类的方法,加上缩进,返回字符串 return reparsed.toprettysml(indent='\t') new_str = prettify(root) with open('sample','w') as f:
def createEmptyMap(publication_id,model_id): root = ET.element('xml') publication = ET.SubElement(root,'publication',attrib = {'id':publication_id}) model = ET.SubElement(publication,'model',attrib={'id':model_id}) tree = ET.ElementTree(root) return tree