Skip to content

Commit a871180

Browse files
committed
fixed minor issues with jsonConverter
1 parent 1e7747a commit a871180

2 files changed

Lines changed: 216 additions & 12 deletions

File tree

samples/new.json

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"users": {
3+
"user": [
4+
{
5+
"id": "1",
6+
"name": "Ahmed Ali",
7+
"posts": {
8+
"post": [
9+
{
10+
"body": "How solar energy is changing the world.",
11+
"topics": {
12+
"topic": [
13+
"solar_energy",
14+
"technology"
15+
]
16+
}
17+
},
18+
{
19+
"body": "The importance of financial literacy in today's economy.",
20+
"topics": {
21+
"topic": "economy"
22+
}
23+
}
24+
]
25+
},
26+
"followers": {
27+
"follower": [
28+
{
29+
"id": "2"
30+
},
31+
{
32+
"id": "3"
33+
},
34+
{
35+
"id": "4"
36+
}
37+
]
38+
}
39+
},
40+
{
41+
"id": "2",
42+
"name": "Yasser Ahmed",
43+
"posts": {
44+
"post": {
45+
"body": "Education reforms: What should come next?",
46+
"topics": {
47+
"topic": [
48+
"education",
49+
"policy"
50+
]
51+
}
52+
}
53+
},
54+
"followers": {
55+
"follower": [
56+
{
57+
"id": "1"
58+
},
59+
{
60+
"id": "5"
61+
}
62+
]
63+
}
64+
},
65+
{
66+
"id": "3",
67+
"name": "Mohamed Sherif",
68+
"posts": {
69+
"post": [
70+
{
71+
"body": "The rise of AI in sports analytics.",
72+
"topics": {
73+
"topic": [
74+
"sports",
75+
"technology"
76+
]
77+
}
78+
},
79+
{
80+
"body": "Top 10 moments in football history.",
81+
"topics": {
82+
"topic": "sports"
83+
}
84+
}
85+
]
86+
},
87+
"followers": {
88+
"follower": [
89+
{
90+
"id": "1"
91+
},
92+
{
93+
"id": "2"
94+
}
95+
]
96+
}
97+
},
98+
{
99+
"id": "4",
100+
"name": "Sara Mahmoud",
101+
"posts": {
102+
"post": {
103+
"body": "The impact of clean water initiatives in rural areas.",
104+
"topics": {
105+
"topic": [
106+
"environment",
107+
"policy"
108+
]
109+
}
110+
}
111+
},
112+
"followers": {
113+
"follower": [
114+
{
115+
"id": "1"
116+
},
117+
{
118+
"id": "3"
119+
},
120+
{
121+
"id": "5"
122+
}
123+
]
124+
}
125+
},
126+
{
127+
"id": "5",
128+
"name": "Omar Khaled",
129+
"posts": {
130+
"post": [
131+
{
132+
"body": "Advancements in renewable energy technologies.",
133+
"topics": {
134+
"topic": [
135+
"technology",
136+
"solar_energy"
137+
]
138+
}
139+
},
140+
{
141+
"body": "Breaking down the future of urban planning.",
142+
"topics": {
143+
"topic": [
144+
"policy",
145+
"economy"
146+
]
147+
}
148+
}
149+
]
150+
},
151+
"followers": {
152+
"follower": {
153+
"id": "2"
154+
}
155+
}
156+
},
157+
{
158+
"id": "6",
159+
"name": "Fatima Saleh",
160+
"posts": {
161+
"post": [
162+
{
163+
"body": "How to balance work and personal life effectively.",
164+
"topics": {
165+
"topic": "self_help"
166+
}
167+
},
168+
{
169+
"body": "Improving productivity with modern tools.",
170+
"topics": {
171+
"topic": [
172+
"technology",
173+
"self_help"
174+
]
175+
}
176+
}
177+
]
178+
},
179+
"followers": {
180+
"follower": [
181+
{
182+
"id": "3"
183+
},
184+
{
185+
"id": "5"
186+
},
187+
{
188+
"id": "4"
189+
}
190+
]
191+
}
192+
}
193+
]
194+
}
195+
}

src/modules/xml_to_json.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def tokenize(xml_string):
2424

2525
# Clean and filter tokens
2626
tokens = [token.strip() for token in tokens if token.strip()]
27-
2827
return tokens
2928

3029
@staticmethod
@@ -103,28 +102,38 @@ def __init__(self, input_file):
103102
def convert_element(self, node):
104103
"""
105104
Convert a node to JSON-compatible dictionary
106-
107105
Args:
108106
node (CustomTreeNode): Node to convert
109-
110107
Returns:
111108
dict: JSON-compatible representation
112109
"""
110+
# If the node has no attributes or children, and only text, return the text directly
111+
if not node.attributes and not node.children and node.text:
112+
return node.text
113+
113114
# Start with attributes
114115
json_data = dict(node.attributes)
115116

116-
# Add text if exists
117-
if node.text:
117+
# Add text if exists and there are children (handle inline text)
118+
if node.text and node.children:
118119
json_data['text'] = node.text
120+
elif node.text: # No children, treat text as direct value
121+
return node.text
119122

120123
# Process children
121124
for child in node.children:
122125
if child.tag not in json_data:
123126
json_data[child.tag] = []
124127
json_data[child.tag].append(self.convert_element(child))
125128

129+
# Collapse lists with a single item to simplify structure
130+
for key, value in json_data.items():
131+
if isinstance(value, list) and len(value) == 1:
132+
json_data[key] = value[0]
133+
126134
return json_data
127135

136+
128137
def _build_tree(self, tokens):
129138
"""
130139
Build a tree from XML tokens
@@ -148,7 +157,6 @@ def _build_tree(self, tokens):
148157
try:
149158
while tokens:
150159
token = tokens.pop(0)
151-
152160
# Debugging: track current context
153161
self._debug_info['current_context'] = [node.tag for node in stack]
154162

@@ -244,13 +252,14 @@ def convert(self, output_path):
244252

245253
# Tokenize XML
246254
tokens = self._parser.tokenize(xml_content)
255+
247256

248257
# Build tree
249258
root = self._build_tree(tokens)
250-
251-
# Convert to JSON
252-
json_data = self.convert_element(root)
253-
259+
260+
# Convert to JSON with root tag as key
261+
json_data = {root.tag: self.convert_element(root)}
262+
254263
# Write JSON
255264
with open(output_path, 'w', encoding='utf-8') as f:
256265
json.dump(json_data, f, indent=4)
@@ -265,8 +274,8 @@ def convert(self, output_path):
265274
# Example usage
266275
# if __name__ == '__main__':
267276
# try:
268-
# converter = XMLToJSONConverter('./samples/sample.xml')
269-
# converter.convert('./samples/sample.json')
277+
# converter = XMLToJSONConverter(r'path_to_file')
278+
# converter.convert(r'path_to_file')
270279
# except Exception as e:
271280
# # User-friendly error message
272281
# print("Oops! Something went wrong during the XML to JSON conversion. Please check your XML file for errors.")

0 commit comments

Comments
 (0)