import re RE_XML_ENCODING = re.compile("encoding[ \t]*=[ \t]*\"([^\"]+)\"") RE_XML_POSITION = re.compile("(\w+)\[(\d+)\]") def getXMLEncoding(data): head = data[:100] headline = head.splitlines()[0] mo = RE_XML_ENCODING.search(headline) return mo.group(1) if mo else None def decodeXML(data): head = data[:100] headline = head.splitlines()[0] mo = RE_XML_ENCODING.search(headline) if mo: encoding = mo.group(1) return data[len(headline):].decode(encoding) else: return data def xml_dom_Document_getElementByPath(doc, path): positions = path.split("/")[1:] if path[0] == "/" else path.split("/") for index, position in enumerate(positions): mo = RE_XML_POSITION.search(position) name = mo.group(1) index = int(mo.group(2))-1 docs = doc.getElementsByTagName(name) if docs: doc = docs[index] else: raise IndexError, "UNKNOWN_POSITION: %s" % ".".join(positions[:index]) return doc def xml_dom_Document_newNode(doc, tagName, attrs): newNode = doc.createElement(tagName) if type(attrs) is list: for key, value in attrs: newNode.setAttribute(key, value) else: key, value = attrs newNode.setAttribute(key, value) return newNode def xml_dom_Document_appendElement(doc, path, tagName, attrs): targetNode = xml_dom_Document_getElementByPath(doc, path) if path else doc targetNode.appendChild(xml_dom_Document_newNode(doc, tagName, attrs)) def xml_dom_Document_removeElement(doc, path): targetNode = xml_dom_Document_getElementByPath(doc, path) targetNode.parentNode.removeChild(targetNode) def xml_dom_Document_getElementByAttr(doc, path, tagName, attr): key, value = attr targetNode = xml_dom_Document_getElementByPath(doc, path) if path else doc for childNode in targetNode.getElementsByTagName(tagName): if childNode.getAttribute(key) == value: return childNode def xml_dom_Document_removeElementByAttr(doc, path, tagName, attr): delNode = xml_dom_Document_getElementByAttr(doc, path, tagName, attr) if delNode: delNode.parentNode.removeChild(delNode) def xml_dom_Document_setElementAttr(doc, path, key, value): targetNode = xml_dom_Document_getElementByPath(doc, path) if path else None targetNode.setAttribute(key, value) def xml_dom_Document_replaceElementByAttr(doc, path, tagName, key, oldValue, newValue=None): newValue = newValue if newValue else oldValue targetNode = xml_dom_Document_getElementByPath(doc, path) if path else doc for childNode in targetNode.getElementsByTagName(tagName): curValue = childNode.getAttribute(key) if curValue == oldValue: childNode.setAttribute(key, newValue) return childNode elif curValue == newValue: return childNode else: newNode = xml_dom_Document_newNode(doc, tagName, [(key, newValue)]) targetNode.appendChild(newNode) return newNode