def __init__(self): self.logger = logging.getLogger('Show') self.logger.info('In configuration.init') self.logger.info('message from configuration') self.settings = {} tree = ET.parse(CFG_PATH) self.doc = tree.getroot() self.logger.debug(ET.tostring(self.doc)) #print('Root tag: {0}'.format(self.doc.tag)) #print('{0} attribs: {1}'.format(self.doc.tag, self.doc.attrib)) self.logger.debug('Root tag: {0}'.format(self.doc.tag)) self.logger.debug('{0} attribs: {1}'.format(self.doc.tag, self.doc.attrib)) self.cfgdict = self.toDict() return
result = { "$" : parseChildren(x.getchildren()) } else: result = dict(prepend.items() + { "$" : parseChildren(x.getchildren()) }.items()) if tag in final: if type(final[tag]) is not types.ListType: final[tag] = [final[tag]] final[tag].append(result) else: final[tag] = result return final #print parseChildren([ET.XML('<tag>txt-value</tag>')]) #print parseChildren([ET.XML('<tag><tag2>txt-value</tag2></tag>')]) #print parseChildren([ET.XML('<tag><tag2>txt-value1</tag2><tag2>txt-value2</tag2></tag>')]) #print parseChildren([ET.XML('<tag ns="ns-value" />')]) #print parseChildren([ET.XML('<tag xmlns:ns="ns-value" />')]) #print parseChildren([ET.XML('<tag xmlns="root-value" xmlns:ns="ns-value" />')]) #print parseChildren([ET.XML('<ns:tag attr="attr-value" />')]) #print parseChildren([ET.XML('<tag attr="attr-value">txt-value</tag>')]) #print parseChildren([ET.XML('<ns:tag attr="attr-value">txt-value</tag>')]) import sys x = ET.parse(sys.argv[1]).getroot() print parseChildren([x]) print ET.tostring(x)
elem = ET.SubElement(root, "attackpoints") elem.text = "2" elem = ET.SubElement(root, "keeperpoints") elem.text = "3" #for child in root: # print (child.tag, child.attrib) outFilename = "./output.xml" html = ET.Element("html") body = ET.SubElement(html, "body") ET.ElementTree(root).write(outFilename) #ET.ElementTree(root).prettyprint(outFilename) print(ET.tostring(root, pretty_print=True)) filename = "./TeamA.xml" tree = ET.parse(filename) elem = tree.getroot() print ("------- child level by child level ------") print("elem.tag=",elem.tag, " elem.attrib=", elem.attrib) for child in elem: if child.tag == "players" : print ("child.tag=", child.tag, " | child.attrib=",child.attrib, " | child.text=", child.text) for subchild in child: print ("subchild.tag=", subchild.tag,