Skip to content

Commit

Permalink
feat(common): add xml utilities to remove or check if a node is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft authored Jul 3, 2022
1 parent f1a5229 commit d07b827
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/common/src/lib/core/utils/xml-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class XmlBuilder {
}

export function readXml(xmlContent: string, ignoreNamespace = true): XMLBuilder {

if(!xmlContent) {
throw new Error('Cannot read XML, provided content is empty');
}

return create(ignoreNamespace ? {
defaultNamespace: {
ele: null,
Expand Down Expand Up @@ -77,6 +82,11 @@ export function newXmlNode(content: { [key: string]: any } | string): XMLBuilder
export function addXmlNode(target: XMLBuilder, node: { [key: string]: any } | string) {
return target.import(newXmlNode(node));
}

export function removeXmlNode(nodeToRemove: XMLBuilder): XMLBuilder {
return nodeToRemove.remove();
}

export function addXmlElement(target: XMLBuilder, ...elements: (string | {name: string, attributes?: {[key: string]: any} })[]) {
let result = target;

Expand All @@ -86,6 +96,14 @@ export function addXmlElement(target: XMLBuilder, ...elements: (string | {name:
return result;
}

export function isXmlNodeEmpty(xml: XMLBuilder): boolean {
try {
return !!xml.first();
} catch (error) {
return true;
}
}

function asXmlNode(xml: XMLBuilder): Node {
return xml.node as unknown as Node;
}
Expand Down

0 comments on commit d07b827

Please sign in to comment.