Parsing XML using's Python's ElementTree


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. from xml.etree import ElementTree as ET
  2. import re
  3.  
  4. def strip_whitespace(my_string):
  5. """Removes spaces, tabs, and newline characters from a string.
  6. \s matches any whitespace character, this is equivalent to the class [\t\n\r\f\v]."""
  7. return re.sub("\s", "", my_string)
  8.  
  9. my_xml = """
  10. <root>
  11. <child>One</child>
  12. <child>Two</child>
  13. </root>
  14. """
  15.  
  16. my_xml = strip_whitespace(my_xml)
  17.  
  18. element = ET.XML(my_xml)
  19.  
  20. for subelement in element:
  21. print subelement.text

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.