Python Forum
BeautifulSoup n levels of nested xml elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BeautifulSoup n levels of nested xml elements
#1
I am trying to read a xml file and create a dictionary object
this is my xml 4 levels of elements
Output:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <Drivetrain> <Element name="PowerUnit"> <Element name="Transmission"> <Element name="TX1"> </Element> <Element name="TX2"> </Element> </Element> <Element name="Engine"> <Element name="V4"> </Element> <Element name="V6"> </Element> </Element> <Element name="Hybrid"> <Element name=""> </Element> <Element name="Battery"> <Element name ="BattteryA"> <Element name="A1"> </Element> <Element name ="BattteryB"> </Element> <Element name ="BattteryC"> </Element> </Element> </Element> </Element> </Drivetrain>
This is my code
from bs4 import BeautifulSoup


def DesignSpace():
    infile = open("data.xml","r")
    contents = infile.read()
    soup = BeautifulSoup(contents,'xml')
    space = soup.find('Element')
    items = {}
    
    children = space.findChildren("Element" , recursive=False)
    for child in children:
        
        key = child["name"]
        for a in child.findChildren("Element"):
            value=a["name"]
            
            subkey=a.findParent("Element")
            subkey1=subkey["name"]
            items.setdefault(subkey1, []).append(value)
        
            
    
    print(items)

    return items


DesignSpace()
I can only go 1 level down I want to go all the way to the 4th level or nth level
Transmission
Tx1
Tx2
Engine
V4
V6
Reply
#2
I have an lxml package that I am releasing soon. the code is here: https://python-forum.io/Thread-selneium-...0#pid62480
it uses lxml to create a dictionary for an html file. with a slight modification (or maybe none at all), it will work with xml.
Reply


Forum Jump:

User Panel Messages

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