@azure/core-xml
Advanced tools
Comparing version 1.0.0-alpha.20200929.1 to 1.0.0-alpha.20201123.1
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
export { stringifyXML, parseXML } from "./xml"; | ||
export { XML_ATTRKEY, XML_CHARKEY } from "./xml.common"; | ||
//# sourceMappingURL=index.js.map |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
/// <reference lib="dom"/> | ||
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common"; | ||
// tslint:disable-next-line:no-null-keyword | ||
const doc = document.implementation.createDocument(null, null, null); | ||
const parser = new DOMParser(); | ||
export function parseXML(str, opts) { | ||
export function parseXML(str, opts = {}) { | ||
var _a, _b, _c; | ||
try { | ||
const updatedOptions = { | ||
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "", | ||
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false, | ||
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY | ||
}; | ||
const dom = parser.parseFromString(str, "application/xml"); | ||
throwIfError(dom); | ||
let obj; | ||
if (opts && opts.includeRoot) { | ||
obj = domToObject(dom); | ||
if (updatedOptions.includeRoot) { | ||
obj = domToObject(dom, updatedOptions); | ||
} | ||
else { | ||
obj = domToObject(dom.childNodes[0]); | ||
obj = domToObject(dom.childNodes[0], updatedOptions); | ||
} | ||
@@ -49,3 +57,3 @@ return Promise.resolve(obj); | ||
} | ||
function domToObject(node) { | ||
function domToObject(node, options) { | ||
let result = {}; | ||
@@ -61,9 +69,9 @@ const childNodeCount = node.childNodes.length; | ||
if (elementWithAttributes) { | ||
result["$"] = {}; | ||
result[XML_ATTRKEY] = {}; | ||
for (let i = 0; i < elementWithAttributes.attributes.length; i++) { | ||
const attr = elementWithAttributes.attributes[i]; | ||
result["$"][attr.nodeName] = attr.nodeValue; | ||
result[XML_ATTRKEY][attr.nodeName] = attr.nodeValue; | ||
} | ||
if (onlyChildTextValue) { | ||
result["_"] = onlyChildTextValue; | ||
result[options.xmlCharKey] = onlyChildTextValue; | ||
} | ||
@@ -82,3 +90,3 @@ } | ||
if (child.nodeType !== Node.TEXT_NODE) { | ||
const childObject = domToObject(child); | ||
const childObject = domToObject(child, options); | ||
if (!result[child.nodeName]) { | ||
@@ -99,5 +107,10 @@ result[child.nodeName] = childObject; | ||
const serializer = new XMLSerializer(); | ||
export function stringifyXML(content, opts) { | ||
const rootName = (opts && opts.rootName) || "root"; | ||
const dom = buildNode(content, rootName)[0]; | ||
export function stringifyXML(content, opts = {}) { | ||
var _a, _b, _c; | ||
const updatedOptions = { | ||
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "root", | ||
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false, | ||
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY | ||
}; | ||
const dom = buildNode(content, updatedOptions.rootName, updatedOptions)[0]; | ||
return ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + serializer.serializeToString(dom)); | ||
@@ -114,3 +127,3 @@ } | ||
} | ||
function buildNode(obj, elementName) { | ||
function buildNode(obj, elementName, options) { | ||
if (obj === undefined || | ||
@@ -128,3 +141,3 @@ obj === null || | ||
for (const arrayElem of obj) { | ||
for (const child of buildNode(arrayElem, elementName)) { | ||
for (const child of buildNode(arrayElem, elementName, options)) { | ||
result.push(child); | ||
@@ -138,3 +151,3 @@ } | ||
for (const key of Object.keys(obj)) { | ||
if (key === "$") { | ||
if (key === XML_ATTRKEY) { | ||
for (const attr of buildAttributes(obj[key])) { | ||
@@ -144,7 +157,7 @@ elem.attributes.setNamedItem(attr); | ||
} | ||
else if (key === "_") { | ||
else if (key === options.xmlCharKey) { | ||
elem.textContent = obj[key].toString(); | ||
} | ||
else { | ||
for (const child of buildNode(obj[key], key)) { | ||
for (const child of buildNode(obj[key], key, options)) { | ||
elem.appendChild(child); | ||
@@ -151,0 +164,0 @@ } |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import * as xml2js from "xml2js"; | ||
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common"; | ||
// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed | ||
@@ -13,4 +14,3 @@ // by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536 | ||
normalizeTags: false, | ||
attrkey: "$", | ||
charkey: "_", | ||
attrkey: XML_ATTRKEY, | ||
explicitArray: true, | ||
@@ -20,3 +20,3 @@ ignoreAttrs: false, | ||
explicitRoot: true, | ||
validator: null, | ||
validator: undefined, | ||
xmlns: false, | ||
@@ -30,6 +30,6 @@ explicitChildren: false, | ||
strict: true, | ||
attrNameProcessors: null, | ||
attrValueProcessors: null, | ||
tagNameProcessors: null, | ||
valueProcessors: null, | ||
attrNameProcessors: undefined, | ||
attrValueProcessors: undefined, | ||
tagNameProcessors: undefined, | ||
valueProcessors: undefined, | ||
rootName: "root", | ||
@@ -41,3 +41,3 @@ xmldec: { | ||
}, | ||
doctype: null, | ||
doctype: undefined, | ||
renderOpts: { | ||
@@ -65,7 +65,9 @@ pretty: true, | ||
* @param obj JSON object to be converted into XML string | ||
* @param opts Options that govern the parsing of given JSON object | ||
* @param opts Options that govern the XML building of given JSON object | ||
* `rootName` indicates the name of the root element in the resulting XML | ||
*/ | ||
export function stringifyXML(obj, opts) { | ||
xml2jsBuilderSettings.rootName = (opts || {}).rootName; | ||
export function stringifyXML(obj, opts = {}) { | ||
var _a; | ||
xml2jsBuilderSettings.rootName = opts.rootName; | ||
xml2jsBuilderSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY; | ||
const builder = new xml2js.Builder(xml2jsBuilderSettings); | ||
@@ -80,4 +82,6 @@ return builder.buildObject(obj); | ||
*/ | ||
export function parseXML(str, opts) { | ||
xml2jsParserSettings.explicitRoot = !!(opts && opts.includeRoot); | ||
export function parseXML(str, opts = {}) { | ||
var _a; | ||
xml2jsParserSettings.explicitRoot = !!opts.includeRoot; | ||
xml2jsParserSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY; | ||
const xmlParser = new xml2js.Parser(xml2jsParserSettings); | ||
@@ -84,0 +88,0 @@ return new Promise((resolve, reject) => { |
@@ -8,2 +8,13 @@ 'use strict'; | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
/** | ||
* Default key used to access the XML attributes. | ||
*/ | ||
const XML_ATTRKEY = "$"; | ||
/** | ||
* Default key used to access the XML value content. | ||
*/ | ||
const XML_CHARKEY = "_"; | ||
// Copyright (c) Microsoft Corporation. | ||
// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed | ||
@@ -18,4 +29,3 @@ // by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536 | ||
normalizeTags: false, | ||
attrkey: "$", | ||
charkey: "_", | ||
attrkey: XML_ATTRKEY, | ||
explicitArray: true, | ||
@@ -25,3 +35,3 @@ ignoreAttrs: false, | ||
explicitRoot: true, | ||
validator: null, | ||
validator: undefined, | ||
xmlns: false, | ||
@@ -35,6 +45,6 @@ explicitChildren: false, | ||
strict: true, | ||
attrNameProcessors: null, | ||
attrValueProcessors: null, | ||
tagNameProcessors: null, | ||
valueProcessors: null, | ||
attrNameProcessors: undefined, | ||
attrValueProcessors: undefined, | ||
tagNameProcessors: undefined, | ||
valueProcessors: undefined, | ||
rootName: "root", | ||
@@ -46,3 +56,3 @@ xmldec: { | ||
}, | ||
doctype: null, | ||
doctype: undefined, | ||
renderOpts: { | ||
@@ -70,7 +80,9 @@ pretty: true, | ||
* @param obj JSON object to be converted into XML string | ||
* @param opts Options that govern the parsing of given JSON object | ||
* @param opts Options that govern the XML building of given JSON object | ||
* `rootName` indicates the name of the root element in the resulting XML | ||
*/ | ||
function stringifyXML(obj, opts) { | ||
xml2jsBuilderSettings.rootName = (opts || {}).rootName; | ||
function stringifyXML(obj, opts = {}) { | ||
var _a; | ||
xml2jsBuilderSettings.rootName = opts.rootName; | ||
xml2jsBuilderSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY; | ||
const builder = new xml2js.Builder(xml2jsBuilderSettings); | ||
@@ -85,4 +97,6 @@ return builder.buildObject(obj); | ||
*/ | ||
function parseXML(str, opts) { | ||
xml2jsParserSettings.explicitRoot = !!(opts && opts.includeRoot); | ||
function parseXML(str, opts = {}) { | ||
var _a; | ||
xml2jsParserSettings.explicitRoot = !!opts.includeRoot; | ||
xml2jsParserSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY; | ||
const xmlParser = new xml2js.Parser(xml2jsParserSettings); | ||
@@ -106,4 +120,6 @@ return new Promise((resolve, reject) => { | ||
exports.XML_ATTRKEY = XML_ATTRKEY; | ||
exports.XML_CHARKEY = XML_CHARKEY; | ||
exports.parseXML = parseXML; | ||
exports.stringifyXML = stringifyXML; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@azure/core-xml", | ||
"version": "1.0.0-alpha.20200929.1", | ||
"version": "1.0.0-alpha.20201123.1", | ||
"description": "Core library for interacting with XML payloads", | ||
@@ -5,0 +5,0 @@ "sdk-type": "client", |
@@ -7,14 +7,35 @@ /** | ||
*/ | ||
export declare function parseXML(str: string, opts?: { | ||
includeRoot?: boolean; | ||
}): Promise<any>; | ||
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>; | ||
/** | ||
* Converts given JSON object to XML string | ||
* @param obj JSON object to be converted into XML string | ||
* @param opts Options that govern the parsing of given JSON object | ||
* @param opts Options that govern the XML building of given JSON object | ||
* `rootName` indicates the name of the root element in the resulting XML | ||
*/ | ||
export declare function stringifyXML(obj: any, opts?: { | ||
export declare function stringifyXML(obj: any, opts?: XmlOptions): string; | ||
/** | ||
* Default key used to access the XML attributes. | ||
*/ | ||
export declare const XML_ATTRKEY = "$"; | ||
/** | ||
* Default key used to access the XML value content. | ||
*/ | ||
export declare const XML_CHARKEY = "_"; | ||
/** | ||
* Options to govern behavior of xml parser and builder. | ||
*/ | ||
export declare interface XmlOptions { | ||
/** | ||
* indicates the name of the root element in the resulting XML when building XML. | ||
*/ | ||
rootName?: string; | ||
}): string; | ||
/** | ||
* indicates whether the root element is to be included or not in the output when parsing XML. | ||
*/ | ||
includeRoot?: boolean; | ||
/** | ||
* key used to access the XML value content when parsing XML. | ||
*/ | ||
xmlCharKey?: string; | ||
} | ||
export {}; |
@@ -8,5 +8,3 @@ | ||
*/ | ||
export declare function parseXML(str: string, opts?: { | ||
includeRoot?: boolean; | ||
}): Promise<any>; | ||
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>; | ||
@@ -16,9 +14,35 @@ /** | ||
* @param obj JSON object to be converted into XML string | ||
* @param opts Options that govern the parsing of given JSON object | ||
* @param opts Options that govern the XML building of given JSON object | ||
* `rootName` indicates the name of the root element in the resulting XML | ||
*/ | ||
export declare function stringifyXML(obj: any, opts?: { | ||
export declare function stringifyXML(obj: any, opts?: XmlOptions): string; | ||
/** | ||
* Default key used to access the XML attributes. | ||
*/ | ||
export declare const XML_ATTRKEY = "$"; | ||
/** | ||
* Default key used to access the XML value content. | ||
*/ | ||
export declare const XML_CHARKEY = "_"; | ||
/** | ||
* Options to govern behavior of xml parser and builder. | ||
*/ | ||
export declare interface XmlOptions { | ||
/** | ||
* indicates the name of the root element in the resulting XML when building XML. | ||
*/ | ||
rootName?: string; | ||
}): string; | ||
/** | ||
* indicates whether the root element is to be included or not in the output when parsing XML. | ||
*/ | ||
includeRoot?: boolean; | ||
/** | ||
* key used to access the XML value content when parsing XML. | ||
*/ | ||
xmlCharKey?: string; | ||
} | ||
export { } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48280
16
467