Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- directory_name = "C:\apps"
- file_name = "web.config"
- xpath_identifier = '/configuration/applicationSettings/Things/setting[@name="CorsTrustedOrigins"]'
- #contents of the xpath node for reference:
- block = '''
- <setting name="Things" serializeAs="Xml">
- <value>
- <ArrayOfString>
- <string>http://localhost:51363</string>
- <string>http://localhost:3333</string>
- </ArrayOfString>
- </value>
- </setting>
- '''
- file_full_path = os.path.join(directory_name, file_name)
- tree = etree.parse(file_full_path)
- root = tree.getroot()
- etree.tostring(root)
- xpath_identifier = str(xpath_identifier)
- value = root.xpath(xpath_identifier)
- #This successfully prints the element I'm after, so I'm sure my xpath is good:
- etree.tostring(value[0])
- #This is the new xml element I want to replace the current xpath'ed element with:
- newxml = '''
- <setting name="CorsTrustedOrigins" serializeAs="Xml">
- <value>
- <ArrayOfString>
- <string>http://maddafakka</string>
- </ArrayOfString>
- </value>
- </setting>
- '''
- newtree = etree.fromstring(newxml)
- #I've tried this:
- value[0].getparent().replace(value[0], newtree)
- #and this
- value[0] = newtree
- #The value of value[0] gets updated, but the "root document" does not.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement