def set_text(self,text): """Set the text of the given node.""" try: #try to see if a text node is already there, and set it if self.node.childNodes[0].nodeName == u'#text': self.node.childNodes[0].data = unicode(text) return except IndexError: pass #if not, let's make one t = minidom() t.from_string("<a>%s</a>" % text) text_node = t.node.childNodes[0] #now we need to see if the target node has more than one child if len(self.node.childNodes) == 0: #if not just append it self.node.appendChild(text_node) else: #else we need to insert it infront of the current node self.node.insertBefore(text_node,self.node.childNodes[0])
def Element(tag,**kwargs): """Creates an element with given tag.""" return minidom(xml.dom.minidom.parseString("<%s/>"%tag).childNodes[0],**kwargs)
def from_string(xml_str): t = minidom() return t.from_string(xml_str)
pass #if not, let's make one t = minidom() t.from_string("<a>%s</a>" % text) text_node = t.node.childNodes[0] #now we need to see if the target node has more than one child if len(self.node.childNodes) == 0: #if not just append it self.node.appendChild(text_node) else: #else we need to insert it infront of the current node self.node.insertBefore(text_node,self.node.childNodes[0]) text = property(get_text,set_text) def append_node(self,node): """Append a child node.""" #append the child self.node.appendChild(node) if __name__ == '__main__': import pdb t = minidom() t.parse('test.xml') pdb.set_trace()