Created
May 29, 2015 18:17
-
-
Save clintmjohnson/8e40178c073a61ba57ad to your computer and use it in GitHub Desktop.
This is an XML Untangle Example
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
| #!/usr/bin/env python | |
| """ | |
| Usage examples for untangle | |
| """ | |
| import untangle | |
| def access(): | |
| """ | |
| Shows basic attribute access and node navigation. | |
| """ | |
| o = untangle.parse('<node id="5">This is cdata<subnode value="abc"/></node>') | |
| return ("Node id = %s, subnode value = %s" % | |
| (o.node['id'], o.node.subnode['value'])) | |
| def siblings_list(): | |
| """ | |
| Shows child element iteration | |
| """ | |
| o = untangle.parse(''' | |
| <root> | |
| <child name="Clint"/> | |
| <child name="Emily"/> | |
| <child name="Logan"/> | |
| </root> | |
| ''') | |
| return ','.join([child['name'] for child in o.root.child]) | |
| def access_cdata(): | |
| """ | |
| Shows how to handle CDATA elements | |
| """ | |
| o = untangle.parse('<node id="5">This is cdata<subnode value="abc"/></node>') | |
| return ("%s" % (o.node.cdata)) | |
| examples = [ | |
| ('Access children with parent.children and' | |
| ' attributes with element["attribute"]', access), | |
| ('Access siblings as list', siblings_list), | |
| ('Access cdata text or other data', access_cdata), | |
| ] | |
| if __name__ == '__main__': | |
| for description, func in examples: | |
| print '=' * 70 | |
| print description | |
| print '=' * 70 | |
| print func() | |
| # vim: set expandtab ts=4 sw=4: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment