File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -94,16 +94,21 @@ def element_to_dict(element):
94
94
if attrs :
95
95
result .update (attrs )
96
96
97
+ children_by_tag = {}
97
98
for child_element in element :
98
- child_key = child_element .tag
99
+ child_key = child_element .tag + "s"
99
100
child_value = element_to_dict (child_element )
100
101
101
- if child_key in result :
102
- if not isinstance (result [child_key ], list ):
103
- result [child_key ] = [result [child_key ]]
104
- result [child_key ].append (child_value )
105
- else :
102
+ # Check if the child element has an 'id' attribute
103
+ if "id" in child_element .attrib :
104
+ # If the child element has an 'id', add it to the result dictionary directly
106
105
result [child_key ] = child_value
106
+ else :
107
+ # If the child element does not have an 'id', represent it as a list
108
+ children_by_tag .setdefault (child_key , []).append (child_value )
109
+
110
+ # Append the lists to the result dictionary
111
+ result .update (children_by_tag )
107
112
108
113
return result
109
114
You can’t perform that action at this time.
0 commit comments