< /blockquote>
und tatsächlich habe ich die DOM -Eigenschaften durchgesehen und die Deklaration ist nirgends zu finden. Die Verarbeitungsanweisung scheint in der Dokumentstruktur nicht vorhanden zu sein. Gibt es eine Immobilie, die mir irgendwo fehlt? Vielleicht eines, das das nicht überordnete Dokument als Zeichenfolge enthält? Und wenn ich nicht wissen würde, wie serializeToString () < /code> es tut.
Code: Select all
const xml = ``;
const dom = new DOMParser().parseFromString(xml, 'text/xml');
const str = new XMLSerializer().serializeToString(dom);
console.log("output of serializeToString", str);
const serialize = function(xml) {
let string = "";
const recursive = function(node,indent = 0) {
if (node.nodeType === Node.ELEMENT_NODE) {
string += "\t".repeat(indent) + "";
if (Array.from(node.childNodes).some(child => child.nodeType === Node.TEXT_NODE && child.data.match(/[^\s]/))) {
for (const child of node.childNodes) {
recursive(child,0);
}
} else {
string += "\n";
for (const child of Array.from(node.childNodes).filter(child => child.nodeType === Node.ELEMENT_NODE)) {
recursive(child,indent + 1);
}
string += "\t".repeat(indent);
}
string += "";
} else {
string += "/>";
}
string += (indent ? "\n" : "");
}
if (node.nodeType === Node.ATTRIBUTE_NODE) {
string += (indent ? "\n" : " ") + "\t".repeat(indent) + `${node.name}="${node.value}"`;
}
if (node.nodeType === Node.TEXT_NODE) {
string += node.data;
}
if (node.nodeType === Node.CDATA_SECTION_NODE) {
string += "" + "\n";
}
if (node.nodeType === Node.PROCESSING_INSTRUCTION_NODE) {
string += "" + "\n";
}
if (node.nodeType === Node.COMMENT_NODE) {
string += "" + "\n";
}
if (node.nodeType === Node.DOCUMENT_NODE) {
for (const child of node.childNodes) {
recursive(child);
}
}
}
recursive(xml);
return string;
}
const output = serialize(dom);
console.log("output of my function", output);