Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imtrinity94/8903856ca84b2e781d85004236d3d013 to your computer and use it in GitHub Desktop.
Save imtrinity94/8903856ca84b2e781d85004236d3d013 to your computer and use it in GitHub Desktop.
Parse strings from an exported vRO Configuration Element with Python
#!/usr/bin/env python3
import argparse
import json
import sys
import xml.etree.ElementTree as ET
parser = argparse.ArgumentParser('Parse strings from exported vRO Configuration Element XML')
parser.add_argument('FILE',help='ConfigElement XML file path')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-n','--name',dest='vroAttribute',help='Attribute Name to parse')
group.add_argument('--json',dest='jsonOut',action='store_true',help='Parse Configuration Element as a JSON object')
cliArgs = parser.parse_args()
tree = ET.parse(cliArgs.FILE)
vroAttribute = cliArgs.vroAttribute
root = tree.getroot()
ourConfig = {}
for attr in root.iter('att'):
attributeName = attr.get('name')
attributeType = attr.get('type')
if attributeType == 'Array/string':
encodedValues = attr.find('value').text
encodedValues = encodedValues.replace('"','\\"')
encodedValues = encodedValues.replace('#;#string#','","')
encodedValues = encodedValues.replace('#}#','"]')
jsonValues = encodedValues.replace('#{#string#','["')
ourConfig[attributeName] = json.loads(jsonValues)
elif attributeType == 'string':
ourConfig[attributeName] = attr.find('value').text
if cliArgs.jsonOut:
print(json.dumps(ourConfig))
else:
if isinstance(ourConfig[vroAttribute],list):
for attr in ourConfig[vroAttribute]:
print(attr)
else:
print(ourConfig[vroAttribute])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment