Skip to content

Commit f3fdabc

Browse files
committed
feat(xml-builder): restore fast-xml-parser in Node.js
1 parent e7206cc commit f3fdabc

File tree

4 files changed

+17
-105
lines changed

4 files changed

+17
-105
lines changed

packages/xml-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "XML builder for the AWS SDK",
55
"dependencies": {
66
"@smithy/types": "^4.5.0",
7-
"@xmldom/xmldom": "0.8.11",
7+
"fast-xml-parser": "5.2.5",
88
"tslib": "^2.6.2"
99
},
1010
"scripts": {

packages/xml-builder/src/xml-parser.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ describe("xml parsing", () => {
8989
</XmlEmptyMapsResult>
9090
</XmlEmptyMapsResponse>`;
9191
const object = parse(xml);
92-
expect(object).toEqual({
92+
expect(object).toMatchObject({
9393
XmlEmptyMapsResponse: {
9494
xmlns: "https://example.com/",
9595
XmlEmptyMapsResult: {
96-
myMap: "\n ",
96+
myMap: /\s*/,
9797
},
9898
},
9999
});
Lines changed: 13 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,17 @@
1-
//
2-
//
3-
// const parser = new XMLParser({
4-
// attributeNamePrefix: "",
5-
// htmlEntities: true,
6-
// ignoreAttributes: false,
7-
// ignoreDeclaration: true,
8-
// parseTagValue: false,
9-
// trimValues: false,
10-
// tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
11-
// });
12-
// parser.addEntity("#xD", "\r");
13-
// parser.addEntity("#10", "\n");
14-
15-
// export function parseXML(xmlString: string): any {
16-
// return parser.parse(xmlString, true);
17-
// }
18-
19-
// temporary replacement for compatibility testing.
20-
import { DOMParser } from "@xmldom/xmldom";
21-
const parser = new DOMParser({
22-
locator: {},
23-
errorHandler: (err) => {
24-
throw new Error(err);
25-
},
1+
import { XMLParser } from "fast-xml-parser";
2+
3+
const parser = new XMLParser({
4+
attributeNamePrefix: "",
5+
htmlEntities: true,
6+
ignoreAttributes: false,
7+
ignoreDeclaration: true,
8+
parseTagValue: false,
9+
trimValues: false,
10+
tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
2611
});
12+
parser.addEntity("#xD", "\r");
13+
parser.addEntity("#10", "\n");
2714

28-
/**
29-
* Cases where this differs from fast-xml-parser:
30-
*
31-
* 1. mixing text with nested tags
32-
* <mixed-text> hello, <bold>world</bold>, how are you?</mixed-text>
33-
*
34-
* @internal
35-
*/
3615
export function parseXML(xmlString: string): any {
37-
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
38-
39-
if (xmlDocument.getElementsByTagName("parsererror").length > 0) {
40-
throw new Error("DOMParser XML parsing error.");
41-
}
42-
43-
// Recursive function to convert XML nodes to JS object
44-
const xmlToObj = (node: Node): any => {
45-
if (node.nodeType === 3) {
46-
if (node.textContent?.trim()) {
47-
return node.textContent;
48-
}
49-
}
50-
51-
if (node.nodeType === 1) {
52-
const element = node as Element;
53-
if (element.attributes.length === 0 && element.childNodes.length === 0) {
54-
return "";
55-
}
56-
57-
const obj: any = {};
58-
59-
const attributes = Array.from(element.attributes);
60-
for (const attr of attributes) {
61-
obj[`${attr.name}`] = attr.value;
62-
}
63-
64-
const childNodes = Array.from(element.childNodes);
65-
for (const child of childNodes) {
66-
const childResult = xmlToObj(child);
67-
68-
if (childResult != null) {
69-
const childName = child.nodeName;
70-
71-
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
72-
return childResult;
73-
}
74-
75-
if (obj[childName]) {
76-
if (Array.isArray(obj[childName])) {
77-
obj[childName].push(childResult);
78-
} else {
79-
obj[childName] = [obj[childName], childResult];
80-
}
81-
} else {
82-
obj[childName] = childResult;
83-
}
84-
} else if (childNodes.length === 1 && attributes.length === 0) {
85-
return element.textContent;
86-
}
87-
}
88-
89-
return obj;
90-
}
91-
92-
return null;
93-
};
94-
95-
return {
96-
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
97-
};
16+
return parser.parse(xmlString, true);
9817
}

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25007,9 +25007,9 @@ __metadata:
2500725007
dependencies:
2500825008
"@smithy/types": "npm:^4.5.0"
2500925009
"@tsconfig/recommended": "npm:1.0.1"
25010-
"@xmldom/xmldom": "npm:0.8.11"
2501125010
concurrently: "npm:7.0.0"
2501225011
downlevel-dts: "npm:0.10.1"
25012+
fast-xml-parser: "npm:5.2.5"
2501325013
rimraf: "npm:3.0.2"
2501425014
tslib: "npm:^2.6.2"
2501525015
typescript: "npm:~5.8.3"
@@ -30538,13 +30538,6 @@ __metadata:
3053830538
languageName: node
3053930539
linkType: hard
3054030540

30541-
"@xmldom/xmldom@npm:0.8.11":
30542-
version: 0.8.11
30543-
resolution: "@xmldom/xmldom@npm:0.8.11"
30544-
checksum: 10c0/e768623de72c95d3dae6b5da8e33dda0d81665047811b5498d23a328d45b13feb5536fe921d0308b96a4a8dd8addf80b1f6ef466508051c0b581e63e0dc74ed5
30545-
languageName: node
30546-
linkType: hard
30547-
3054830541
"@xtuc/ieee754@npm:^1.2.0":
3054930542
version: 1.2.0
3055030543
resolution: "@xtuc/ieee754@npm:1.2.0"

0 commit comments

Comments
 (0)