Python Forum
Parse XML - how to handle deep levels/hierarchy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse XML - how to handle deep levels/hierarchy
#6
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; ------------------------------------------------------------------------------------------
Reply


Messages In This Thread
RE: Parse XML - how to handle deep levels/hierarchy - by Larz60+ - Apr-17-2018, 12:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  deep learning python Stevedas 1 1,733 Sep-26-2021, 08:32 AM
Last Post: Yoriz
  How can i generate hierarchy directory Anldra12 2 2,003 Jun-05-2021, 08:28 AM
Last Post: Anldra12
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,055 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 3,982 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  Relative import multiple levels ? Windspar 3 4,492 Feb-02-2018, 11:55 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020