I am currently trying to load an xml file and modify the text inside a pair of xml tags, like this:
sometext
actually minidom is no more difficult to use than other dom parsers, if you dont like it you may want to consider complaining to the w3c
from xml.dom.minidom import parseString
XML = """
<nodeA>
<nodeB>Text hello</nodeB>
<nodeC><noText></noText></nodeC>
</nodeA>
"""
def replaceText(node, newText):
if node.firstChild.nodeType != node.TEXT_NODE:
raise Exception("node does not contain text")
node.firstChild.replaceWholeText(newText)
def main():
doc = parseString(XML)
node = doc.getElementsByTagName('nodeB')[0]
replaceText(node, "Hello World")
print doc.toxml()
try:
node = doc.getElementsByTagName('nodeC')[0]
replaceText(node, "Hello World")
except:
print "error"
if __name__ == '__main__':
main()