UNPKG

6.71 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license.
3Object.defineProperty(exports, "__esModule", { value: true });
4/**
5 * Given an xml element, returns the value of the attribute with the specified name.
6 * @param xml Xml object
7 * @param name Attribute name
8 * @returns The attribute value or undefined
9 * @example Given the the following xml, the attribute name "DefaultValue" will return the value "abc".
10 * <First DefaultValue="abc">1</First>
11 */
12function getXmlAttributeValue(xml, name) {
13 try {
14 return xml.$[name];
15 }
16 catch (err) {
17 // reading xml values is resilient to errors but you can uncomment the next line for debugging if attributes are missing
18 // console.error(`Unable to get xml attribute value "${name}". ${err}`);
19 }
20}
21exports.getXmlAttributeValue = getXmlAttributeValue;
22/**
23 * Given an xml object, returns the first inner element with the specified name, or undefined.
24 * @param xml Xml object
25 * @param name Element name
26 * @returns Xml object or undefined
27 * @example Given the the following xml, the name "Second" will return the xml object for <Second>...</Second>.
28 * <Current>
29 * <First>1</First>
30 * <Second>2</Second>
31 * </Current>
32 */
33function getXmlElement(xml, name) {
34 try {
35 const element = xml[name];
36 if (element instanceof Array) {
37 return element[0];
38 }
39 }
40 catch (err) {
41 // reading xml values is resilient to errors but you can uncomment the next line for debugging if elements are missing
42 // console.error(`Unable to get xml element "${name}". ${err}`);
43 }
44}
45exports.getXmlElement = getXmlElement;
46/**
47 * Given an xml object, returns the attribute value for the first inner element with the specified name, or undefined.
48 * @param xml Xml object
49 * @param elementName Element name
50 * @param attributeName Attribute name
51 * @example Given the the following xml, the element name "First" and attribute name "DefaultValue" will return the value "abc".
52 * <Current>
53 * <First DefaultValue="abc">1</First>
54 * </Current>
55 */
56function getXmlElementAttributeValue(xml, elementName, attributeName = "DefaultValue") {
57 const element = getXmlElement(xml, elementName);
58 if (element) {
59 return getXmlAttributeValue(element, attributeName);
60 }
61}
62exports.getXmlElementAttributeValue = getXmlElementAttributeValue;
63/**
64 * Given an xml object, returns an array with the inner elements with the specified name.
65 * @param xml Xml object
66 * @param name Element name
67 * @returns Array of xml objects;
68 * @example Given the the following xml, the name "Item" will return an array with the two items.
69 * <Items>
70 * <Item>1</Item>
71 * <Item>2</Item>
72 * </Items>
73 */
74function getXmlElements(xml, name) {
75 try {
76 const elements = xml[name];
77 return (elements instanceof Array) ? elements : [];
78 }
79 catch (err) {
80 return [];
81 }
82}
83exports.getXmlElements = getXmlElements;
84/**
85 * Given an xml object, for the specified element, returns the values of the inner elements with the specified item element name.
86 * @param xml The xml object.
87 * @param name The name of the inner xml element.
88 * @example Given the the following xml, the container name "Items" and item name "Item" will return ["One", "Two"].
89 * If the attributeName is "AnotherValue", then it will return ["First", "Second"].
90 * <Items>
91 * <Item DefaultValue="One" AnotherValue="First">1</Item>
92 * <Item DefaultValue="Two" AnotherValue="Second">2</Item>
93 * </Current>
94 */
95function getXmlElementsAttributeValue(xml, name, itemElementName, attributeName = "DefaultValue") {
96 const values = [];
97 try {
98 const xmlElements = xml[name][0][itemElementName];
99 xmlElements.forEach((xmlElement) => {
100 const elementValue = getXmlAttributeValue(xmlElement, attributeName);
101 if (elementValue !== undefined) {
102 values.push(elementValue);
103 }
104 });
105 }
106 catch (err) {
107 // do nothing
108 }
109 return values;
110}
111exports.getXmlElementsAttributeValue = getXmlElementsAttributeValue;
112/**
113 * Given an xml object, for the specified element, returns the values of the inner elements with the specified item element name.
114 * @param xml The xml object.
115 * @param name The name of the inner xml element.
116 * @example Given the the following xml, the container name "Items" and item name "Item" will return ["1", "2"].
117 * <Items>
118 * <Item>1</Item>
119 * <Item>2</Item>
120 * </Current>
121 */
122function getXmlElementsValue(xml, name, itemElementName) {
123 const values = [];
124 getXmlElements(xml, name).forEach((xmlElement) => {
125 const elementValue = getXmlElementValue(xmlElement, itemElementName);
126 if (elementValue !== undefined) {
127 values.push(elementValue);
128 }
129 });
130 return values;
131}
132exports.getXmlElementsValue = getXmlElementsValue;
133/**
134 * Returns the value of the first inner xml element with the specified name.
135 * @param xml The xml object.
136 * @param name The name of the inner xml element.
137 * @example Given the the following xml, the name "Second" will return the value "2".
138 * <Current>
139 * <First>1</First>
140 * <Second>2</Second>
141 * </Current>
142 */
143function getXmlElementValue(xml, name) {
144 try {
145 const element = xml[name];
146 if (element instanceof Array) {
147 return element[0];
148 }
149 }
150 catch (err) {
151 // reading xml values is resilient to errors but you can uncomment the next line for debugging if elements are missing
152 // console.error(`Unable to get xml element value "${name}". ${err}`);
153 }
154}
155exports.getXmlElementValue = getXmlElementValue;
156/**
157 * Given an xml object, set the attribute value for the specified element name.
158 * @param xml Xml object
159 * @param elementName Element name
160 * @param attributeValue Attribute value
161 * @param attributeName Attribute name
162 */
163function setXmlElementAttributeValue(xml, elementName, attributeValue, attributeName = "DefaultValue") {
164 xml[elementName][0].$[attributeName] = attributeValue;
165}
166exports.setXmlElementAttributeValue = setXmlElementAttributeValue;
167/**
168 * Given an xml object, set the inner xml element
169 * @param xml Xml object
170 * @param elementName Element name
171 * @param elementValue Element value
172 */
173function setXmlElementValue(xml, elementName, elementValue) {
174 xml[elementName] = elementValue;
175}
176exports.setXmlElementValue = setXmlElementValue;
177//# sourceMappingURL=xml.js.map
\No newline at end of file