Last active
October 2, 2015 09:37
-
-
Save AaronPhalen/f8ee906cffcd1f751337 to your computer and use it in GitHub Desktop.
A Basix example on creating xml, reading xml, manipulating xml, and xml io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Aaron Phalen | Twitter: @aaron_phalen | Email: [email protected] | |
# Example of parsing xml in python with various librarie: read, write, modify, io | |
try: | |
import xml.etree.cElementTree as ET | |
except: | |
import xml.etree.ElementTree as ET | |
from xml.dom import minidom | |
# 1. Create Xml with ET | |
root = ET.Element("root") | |
ET.SubElement(root, "child1").text = "Child 1" | |
xml_doc = ET.tostring(root) | |
# Output | |
print xml_doc | |
# <root><child1>Child 1</child1></root> | |
xml = minidom.parseString(xml_doc) # minidom.parse(<filename>) for reading xml files | |
root = xml.getElementsByTagName("root")[0].firstChild.data | |
# Ouput | |
print root | |
# | |
child1 = root.getElementsByTagName("child1")[0].firstChild.data | |
# Output | |
# Note: Pythonistas claim xml.dom.minidom parse and parseString is heavy on memory use. | |
# Some alternatives include xml.etree and lxml. Examples to come soon! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment