Skip to content

Commit a5b408b

Browse files
committed
update tests
1 parent 54f5688 commit a5b408b

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/utils/xmlParser.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1+
// src/utils/xmlParser.ts
12
export function parseXmlToJson(xml: string): unknown {
23
const parser = new DOMParser();
34
const doc = parser.parseFromString(xml, 'application/xml');
45

56
const errorNode = doc.querySelector('parsererror');
67
if (errorNode) {
7-
throw new Error('Invalid XML');
8-
}
8+
throw new Error('Invalid XML');}
99

1010
function xmlNodeToJson(node: Element): unknown {
1111
const obj: Record<string, unknown> = {};
1212

13+
// Attributs
1314
for (const attr of Array.from(node.attributes)) {
1415
obj[attr.name] = attr.value;
1516
}
1617

18+
// Enfants
1719
for (const child of Array.from(node.children)) {
1820
const childObj = xmlNodeToJson(child);
19-
if (obj[child.tagName]) {
20-
if (!Array.isArray(obj[child.tagName])) {
21-
obj[child.tagName] = [obj[child.tagName]];
21+
const key = child.tagName;
22+
const existing = obj[key];
23+
24+
if (existing !== undefined) {
25+
if (Array.isArray(existing)) {
26+
existing.push(childObj);
27+
} else {
28+
obj[key] = [existing, childObj];
2229
}
23-
(obj[child.tagName] as unknown[]).push(childObj);
24-
2530
} else {
26-
obj[child.tagName] = childObj;
31+
obj[key] = childObj;
2732
}
2833
}
2934

0 commit comments

Comments
 (0)