Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. directory_name = "C:\apps"
  2. file_name = "web.config"
  3.  
  4. xpath_identifier = '/configuration/applicationSettings/Things/setting[@name="CorsTrustedOrigins"]'
  5.  
  6. #contents of the xpath node for reference:
  7. block = '''
  8. <setting name="Things" serializeAs="Xml">
  9. <value>
  10. <ArrayOfString>
  11. <string>http://localhost:51363</string>
  12. <string>http://localhost:3333</string>
  13. </ArrayOfString>
  14. </value>
  15. </setting>
  16. '''
  17.  
  18. file_full_path = os.path.join(directory_name, file_name)
  19. tree = etree.parse(file_full_path)
  20. root = tree.getroot()
  21.  
  22. etree.tostring(root)
  23.  
  24.  
  25. xpath_identifier = str(xpath_identifier)
  26.  
  27. value = root.xpath(xpath_identifier)
  28.  
  29. #This successfully prints the element I'm after, so I'm sure my xpath is good:
  30. etree.tostring(value[0])
  31.  
  32. #This is the new xml element I want to replace the current xpath'ed element with:
  33. newxml = '''
  34. <setting name="CorsTrustedOrigins" serializeAs="Xml">
  35. <value>
  36. <ArrayOfString>
  37. <string>http://maddafakka</string>
  38. </ArrayOfString>
  39. </value>
  40. </setting>
  41. '''
  42.  
  43. newtree = etree.fromstring(newxml)
  44.  
  45. #I've tried this:
  46. value[0].getparent().replace(value[0], newtree)
  47.  
  48. #and this
  49. value[0] = newtree
  50.  
  51. #The value of value[0] gets updated, but the "root document" does not.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement