Here's a script that will expose the contents of an XML script
without regard to the actual data:
This might prove helpful:
import XMLpaths
from pathlib import PurePath, PosixPath
from lxml import etree as et
import os
class ParseUsing_lxml:
def __init__(self, infile):
self.xpath = XMLpaths.XMLpaths()
self.infile = os.path.abspath(self.xpath.xmlpath / infile)
self.tree = et.parse(self.infile)
self.root = self.tree.getroot()
ptree = et.tostring(self.root, pretty_print=True).decode("utf-8")
# print(ptree)
self.children = []
self.get_children(self.root)
# print(self.children)
self.tree_dict = {}
self.parse_tree()
def get_children(self, root):
for n in range(len(root)):
self.children.append(root[n])
if(len(root[n])):
self.get_children(root[n])
def parse_tree(self):
sep1 = '=' * 90
sep2 = '-' * 90
root = self.root
children = self.children
print('\nRoot:')
print(f'Root attributes: {root.attrib}')
for key, value in root.items():
print(f'key: {key}, vtype: {type(value)}, value: {value}')
print(sep1)
print('Child attributes')
for n, child in enumerate(children):
print(f'Child{n} child: {child}')
print(f'Child{n} attributes: {child.attrib}')
for key, value in child.items():
print(f'key: {key}, vtype: {type(value)}, value: {value}')
print(sep2)
def tryit():
pl = ParseUsing_lxml('ziggy.xml')
if __name__ == '__main__':
tryit()
produces:
Output:
Root:
Root attributes: {'{www.example.com/myExample/Xyz}Id': 'Package', '{www.example.com/myExample/Xyz}CreationDate': '2/21/2018 11:11:48 AM', '{www.example.com/myExample/Xyz}XYZID': '{FB8BE06B-76B6-44DA-B2C7-043BD0989CBF}', '{www.example.com/myExample/Xyz}ObjectName': 'MyTestProject', '{www.example.com/myExample/Xyz}VersionGUID': '{8D9F7CDA-590E-44C3-8896-786D27167F7D}'}
key: {www.example.com/myExample/Xyz}Id, vtype: <class 'str'>, value: Package
key: {www.example.com/myExample/Xyz}CreationDate, vtype: <class 'str'>, value: 2/21/2018 11:11:48 AM
key: {www.example.com/myExample/Xyz}XYZID, vtype: <class 'str'>, value: {FB8BE06B-76B6-44DA-B2C7-043BD0989CBF}
key: {www.example.com/myExample/Xyz}ObjectName, vtype: <class 'str'>, value: MyTestProject
key: {www.example.com/myExample/Xyz}VersionGUID, vtype: <class 'str'>, value: {8D9F7CDA-590E-44C3-8896-786D27167F7D}
==========================================================================================
Child attributes
Child0 child: <Element {www.example.com/myExample/Xyz}Property at 0x2ccbe08>
Child0 attributes: {'{www.example.com/myExample/Xyz}Name': 'PackageFormatVersion'}
key: {www.example.com/myExample/Xyz}Name, vtype: <class 'str'>, value: PackageFormatVersion
------------------------------------------------------------------------------------------
Child1 child: <Element {www.example.com/myExample/Xyz}ConnectionManagers at 0x2ccbe48>
Child1 attributes: {}
------------------------------------------------------------------------------------------
Child2 child: <Element {www.example.com/myExample/Xyz}ConnectionManager at 0x2ccbe88>
Child2 attributes: {'{www.example.com/myExample/Xyz}refId': 'Package.ConnectionManagers[RTG093939BB.AdminDB]', '{www.example.com/myExample/Xyz}CreationName': 'OLEDB', '{www.example.com/myExample/Xyz}XYZID': '{C67B6283-781F-4B0E-A9A7-376A157B6F16}', '{www.example.com/myExample/Xyz}ObjectName': 'RTG093939BB.AdminDB'}
key: {www.example.com/myExample/Xyz}refId, vtype: <class 'str'>, value: Package.ConnectionManagers[RTG093939BB.AdminDB]
key: {www.example.com/myExample/Xyz}CreationName, vtype: <class 'str'>, value: OLEDB
key: {www.example.com/myExample/Xyz}XYZID, vtype: <class 'str'>, value: {C67B6283-781F-4B0E-A9A7-376A157B6F16}
key: {www.example.com/myExample/Xyz}ObjectName, vtype: <class 'str'>, value: RTG093939BB.AdminDB
------------------------------------------------------------------------------------------
Child3 child: <Element {www.example.com/myExample/Xyz}ObjectData at 0x2ccbec8>
Child3 attributes: {}
------------------------------------------------------------------------------------------
Child4 child: <Element {www.example.com/myExample/Xyz}ConnectionManager at 0x2ccbf08>
Child4 attributes: {'{www.example.com/myExample/Xyz}ConnectionString': 'Data Source=RTG093939BB;Initial Catalog=AdminDB;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;'}
key: {www.example.com/myExample/Xyz}ConnectionString, vtype: <class 'str'>, value: Data Source=RTG093939BB;Initial Catalog=AdminDB;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;
------------------------------------------------------------------------------------------
Child5 child: <Element {www.example.com/myExample/Xyz}ConnectionManager at 0x2ccbf88>
Child5 attributes: {'{www.example.com/myExample/Xyz}refId': 'Package.ConnectionManagers[RTG093955XT.Stage]', '{www.example.com/myExample/Xyz}CreationName': 'OLEDB', '{www.example.com/myExample/Xyz}XYZID': '{8B4F57EA-03EA-49FA-B4BD-828A89FE5A32}', '{www.example.com/myExample/Xyz}ObjectName': 'RTG093955XT.Stage'}
key: {www.example.com/myExample/Xyz}refId, vtype: <class 'str'>, value: Package.ConnectionManagers[RTG093955XT.Stage]
key: {www.example.com/myExample/Xyz}CreationName, vtype: <class 'str'>, value: OLEDB
key: {www.example.com/myExample/Xyz}XYZID, vtype: <class 'str'>, value: {8B4F57EA-03EA-49FA-B4BD-828A89FE5A32}
key: {www.example.com/myExample/Xyz}ObjectName, vtype: <class 'str'>, value: RTG093955XT.Stage
------------------------------------------------------------------------------------------
Child6 child: <Element {www.example.com/myExample/Xyz}ObjectData at 0x2ccbfc8>
Child6 attributes: {}
------------------------------------------------------------------------------------------
Child7 child: <Element {www.example.com/myExample/Xyz}ConnectionManager at 0x2cdb048>
Child7 attributes: {'{www.example.com/myExample/Xyz}ConnectionString': 'Data Source=RTG093955XT;Initial Catalog=Stage;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;'}
key: {www.example.com/myExample/Xyz}ConnectionString, vtype: <class 'str'>, value: Data Source=RTG093955XT;Initial Catalog=Stage;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;
------------------------------------------------------------------------------------------