typesxml
Version:
Open source XML library written in TypeScript
91 lines • 2.72 kB
JavaScript
/*******************************************************************************
* Copyright (c) 2023-2026 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-v10.html
*
* Contributors:
* Maxprograms - initial API and implementation
*******************************************************************************/
import { Constants } from "./Constants.js";
import { XMLDeclaration } from "./XMLDeclaration.js";
import { XMLElement } from "./XMLElement.js";
import { XMLUtils } from "./XMLUtils.js";
export class XMLDocument {
content;
documentType;
constructor() {
this.content = new Array();
}
contentIterator() {
return this.content.values();
}
setRoot(root) {
this.content.push(root);
}
getRoot() {
for (let node of this.content) {
if (node instanceof XMLElement) {
return node;
}
}
return undefined;
}
setDocumentType(documentType) {
this.documentType = documentType;
this.content.push(documentType);
}
getDocumentType() {
return this.documentType;
}
setXmlDeclaration(declaration) {
this.content.unshift(declaration);
}
getXmlDeclaration() {
if (this.content[0] instanceof XMLDeclaration) {
return this.content[0];
}
return undefined;
}
addComment(comment) {
this.content.push(comment);
}
addProcessingInstruction(pi) {
this.content.push(pi);
}
addTextNode(node) {
this.content.push(node);
}
getNodeType() {
return Constants.DOCUMENT_NODE;
}
toString() {
let result = '';
let isXml10 = true;
let xmlDeclaration = this.getXmlDeclaration();
if (xmlDeclaration) {
isXml10 = xmlDeclaration.getVersion() === '1.0';
}
this.content.forEach((node) => {
result += isXml10 ? XMLUtils.validXml10Chars(node.toString()) : XMLUtils.validXml11Chars(node.toString());
});
return result;
}
equals(node) {
if (node instanceof XMLDocument) {
if (this.content.length !== node.content.length) {
return false;
}
for (let i = 0; i < this.content.length; i++) {
if (!this.content[i].equals(node.content[i])) {
return false;
}
}
return true;
}
return false;
}
}
//# sourceMappingURL=XMLDocument.js.map