Skip to content

Instantly share code, notes, and snippets.

@Atlas7
Created January 12, 2016 15:27
Show Gist options
  • Save Atlas7/2a99cfdfeab0bb3a7c47 to your computer and use it in GitHub Desktop.
Save Atlas7/2a99cfdfeab0bb3a7c47 to your computer and use it in GitHub Desktop.
Udacity - Web Development - API - Parse XML with Python minidom library

Some sample scripts to demo parsing XML with Python minidom library. Taken from the Udacity Web Development Course - this lesson.

Unknown:apps johnny$ ipython
Python 2.7.10 |Anaconda 2.4.0 (x86_64)| (default, Oct 19 2015, 18:31:17) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from xml.dom import minidom

In [2]: minidom.parseString
Out[2]: <function xml.dom.minidom.parseString>

In [3]: minidom.parseString("<mytag>contents!<children><item>1</item><item>2</item></children></mytag>")
Out[3]: <xml.dom.minidom.Document instance at 0x102cb0a70>

In [4]: x = minidom.parseString("<mytag>contents!<children><item>1</item><item>2</item></children></mytag>")

In [5]: x
Out[5]: <xml.dom.minidom.Document instance at 0x102cd3050>

In [6]: print x
<xml.dom.minidom.Document instance at 0x102cd3050>

In [7]: dir(x)
Out[7]: 
['ATTRIBUTE_NODE',
 'CDATA_SECTION_NODE',
 'COMMENT_NODE',
 'DOCUMENT_FRAGMENT_NODE',
 'DOCUMENT_NODE',
 'DOCUMENT_TYPE_NODE',
 'ELEMENT_NODE',
 'ENTITY_NODE',
 'ENTITY_REFERENCE_NODE',
 'NOTATION_NODE',
 'PROCESSING_INSTRUCTION_NODE',
 'TEXT_NODE',
 '__doc__',
 '__init__',
 '__module__',
 '__nonzero__',
 '_call_user_data_handler',
 '_child_node_types',
 '_create_entity',
 '_create_notation',
 '_elem_info',
 '_get_actualEncoding',
 '_get_async',
 '_get_childNodes',
 '_get_doctype',
 '_get_documentElement',
 '_get_documentURI',
 '_get_elem_info',
 '_get_encoding',
 '_get_errorHandler',
 '_get_firstChild',
 '_get_lastChild',
 '_get_localName',
 '_get_standalone',
 '_get_strictErrorChecking',
 '_get_version',
 '_id_cache',
 '_id_search_stack',
 '_magic_id_count',
 '_set_async',
 'abort',
 'actualEncoding',
 'appendChild',
 'async',
 'attributes',
 'childNodes',
 'cloneNode',
 'createAttribute',
 'createAttributeNS',
 'createCDATASection',
 'createComment',
 'createDocumentFragment',
 'createElement',
 'createElementNS',
 'createProcessingInstruction',
 'createTextNode',
 'doctype',
 'documentElement',
 'documentURI',
 'encoding',
 'errorHandler',
 'firstChild',
 'getElementById',
 'getElementsByTagName',
 'getElementsByTagNameNS',
 'getInterface',
 'getUserData',
 'hasChildNodes',
 'implementation',
 'importNode',
 'insertBefore',
 'isSameNode',
 'isSupported',
 'lastChild',
 'load',
 'loadXML',
 'localName',
 'namespaceURI',
 'nextSibling',
 'nodeName',
 'nodeType',
 'nodeValue',
 'normalize',
 'ownerDocument',
 'parentNode',
 'prefix',
 'previousSibling',
 'removeChild',
 'renameNode',
 'replaceChild',
 'saveXML',
 'setUserData',
 'standalone',
 'strictErrorChecking',
 'toprettyxml',
 'toxml',
 'unlink',
 'version',
 'writexml']

In [8]: x.toprettyxml()
Out[8]: u'<?xml version="1.0" ?>\n<mytag>\n\tcontents!\n\t<children>\n\t\t<item>1</item>\n\t\t<item>2</item>\n\t</children>\n</mytag>\n'

In [9]: print x.toprettyxml()
<?xml version="1.0" ?>
<mytag>
	contents!
	<children>
		<item>1</item>
		<item>2</item>
	</children>
</mytag>


In [10]: 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment