|
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), |
11 |
| -}); |
12 |
| -parser.addEntity("#xD", "\r"); |
13 |
| -parser.addEntity("#10", "\n"); |
| 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 | 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 | + |
| 23 | +/** |
| 24 | + * Cases where this differs from fast-xml-parser: |
| 25 | + * |
| 26 | + * 1. mixing text with nested tags |
| 27 | + * <mixed-text> hello, <bold>world</bold>, how are you?</mixed-text> |
| 28 | + * |
| 29 | + * @internal |
| 30 | + */ |
15 | 31 | export function parseXML(xmlString: string): any {
|
16 |
| - return parser.parse(xmlString, true); |
| 32 | + const xmlDocument = parser.parseFromString(xmlString, "application/xml"); |
| 33 | + |
| 34 | + // Recursive function to convert XML nodes to JS object |
| 35 | + const xmlToObj = (node: Node): any => { |
| 36 | + if (node.nodeType === 3) { |
| 37 | + if (node.textContent?.trim()) { |
| 38 | + return node.textContent; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (node.nodeType === 1) { |
| 43 | + const element = node as Element; |
| 44 | + if (element.attributes.length === 0 && element.childNodes.length === 0) { |
| 45 | + return ""; |
| 46 | + } |
| 47 | + |
| 48 | + const obj: any = {}; |
| 49 | + |
| 50 | + const attributes = Array.from(element.attributes); |
| 51 | + for (const attr of attributes) { |
| 52 | + obj[`${attr.name}`] = attr.value; |
| 53 | + } |
| 54 | + |
| 55 | + const childNodes = Array.from(element.childNodes); |
| 56 | + for (const child of childNodes) { |
| 57 | + const childResult = xmlToObj(child); |
| 58 | + |
| 59 | + if (childResult != null) { |
| 60 | + const childName = child.nodeName; |
| 61 | + |
| 62 | + if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") { |
| 63 | + return childResult; |
| 64 | + } |
| 65 | + |
| 66 | + if (obj[childName]) { |
| 67 | + if (Array.isArray(obj[childName])) { |
| 68 | + obj[childName].push(childResult); |
| 69 | + } else { |
| 70 | + obj[childName] = [obj[childName], childResult]; |
| 71 | + } |
| 72 | + } else { |
| 73 | + obj[childName] = childResult; |
| 74 | + } |
| 75 | + } else if (childNodes.length === 1 && attributes.length === 0) { |
| 76 | + return element.textContent; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return obj; |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + }; |
| 85 | + |
| 86 | + return { |
| 87 | + [xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement), |
| 88 | + }; |
17 | 89 | }
|
0 commit comments