private toFormattedXml()

in src/models/jObject.ts [158:227]


    private toFormattedXml(identation: number = 0, escapeCallbacks?: {
        attribute?: (value: string) => boolean
    }): string {
        let result = "";
        const content = this.value;
        let lineBreak = "\n";

        for (let i = 0; i < identation; i++) {
            lineBreak += " ";
        }

        switch (this.type) {
            case "document":
                this.children.forEach(child => {
                    result += child.toFormattedXml(0, escapeCallbacks) + "\n";
                });
                break;

            case "element":
            case "template": {
                const tag = this.join([this.ns, this.name], ":");

                result += `${lineBreak}<${tag}`;

                this.attributes.forEach(attribute => {
                    let value = attribute.value.toString();
                    value = escapeCallbacks && escapeCallbacks.attribute && !escapeCallbacks.attribute(value) ? value : XmlUtil.encode(value);
                    result += ` ${this.join([attribute.ns, attribute.name], ":")}="${value}"`;
                });

                if (this.children.length > 0) {
                    result += `>`;

                    this.children.forEach(child => {
                        result += child.toFormattedXml(identation + 4, escapeCallbacks);
                    });

                    result += `${lineBreak}</${tag}>`;
                } else if (content) {
                    result += `>${content}</${tag}>`;
                } else {
                    result += ` />`;
                }
                break;
            }

            case "question":
                result += this.value;
                break;

            case "comment":
                result += `${lineBreak}<!--${content}-->`;
                break;

            case "cdata":
                result += `<![CDATA[${content}]]>`;
                break;

            case "text":
                if (content) {
                    result += content;
                }
                break;

            default:
                throw new Error(`Unknown element type ${this.type}.`);
        }

        return result;
    }