|
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), |
26 | 11 | });
|
| 12 | +parser.addEntity("#xD", "\r"); |
| 13 | +parser.addEntity("#10", "\n"); |
27 | 14 |
|
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 |
| - */ |
36 | 15 | 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); |
98 | 17 | }
|
0 commit comments