@aws-sdk/xml-builder
Advanced tools
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.parseXML = parseXML; | ||
| let parser; | ||
| function parseXML(xmlString) { | ||
| if (!parser) { | ||
| parser = new DOMParser(); | ||
| } | ||
| const xmlDocument = parser.parseFromString(xmlString, "application/xml"); | ||
| if (xmlDocument.getElementsByTagName("parsererror").length > 0) { | ||
| throw new Error("DOMParser XML parsing error."); | ||
| } | ||
| const xmlToObj = (node) => { | ||
| if (node.nodeType === Node.TEXT_NODE) { | ||
| if (node.textContent?.trim()) { | ||
| return node.textContent; | ||
| } | ||
| } | ||
| if (node.nodeType === Node.ELEMENT_NODE) { | ||
| const element = node; | ||
| if (element.attributes.length === 0 && element.childNodes.length === 0) { | ||
| return ""; | ||
| } | ||
| const obj = {}; | ||
| const attributes = Array.from(element.attributes); | ||
| for (const attr of attributes) { | ||
| obj[`${attr.name}`] = attr.value; | ||
| } | ||
| const childNodes = Array.from(element.childNodes); | ||
| for (const child of childNodes) { | ||
| const childResult = xmlToObj(child); | ||
| if (childResult != null) { | ||
| const childName = child.nodeName; | ||
| if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") { | ||
| return childResult; | ||
| } | ||
| if (obj[childName]) { | ||
| if (Array.isArray(obj[childName])) { | ||
| obj[childName].push(childResult); | ||
| } | ||
| else { | ||
| obj[childName] = [obj[childName], childResult]; | ||
| } | ||
| } | ||
| else { | ||
| obj[childName] = childResult; | ||
| } | ||
| } | ||
| else if (childNodes.length === 1 && attributes.length === 0) { | ||
| return element.textContent; | ||
| } | ||
| } | ||
| return obj; | ||
| } | ||
| return null; | ||
| }; | ||
| return { | ||
| [xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement), | ||
| }; | ||
| } |
+3
-3
| { | ||
| "name": "@aws-sdk/xml-builder", | ||
| "version": "3.972.22", | ||
| "version": "3.972.23", | ||
| "description": "XML utilities for the AWS SDK", | ||
@@ -48,4 +48,4 @@ "dependencies": { | ||
| "react-native": { | ||
| "./dist-es/xml-parser": "./dist-es/xml-parser", | ||
| "./dist-cjs/xml-parser": "./dist-cjs/xml-parser" | ||
| "./dist-es/xml-parser": "./dist-es/xml-parser.browser", | ||
| "./dist-cjs/xml-parser": "./dist-cjs/xml-parser.browser" | ||
| }, | ||
@@ -52,0 +52,0 @@ "homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages-internal/xml-builder", |
58304
4.04%34
3.03%1407
4.45%