2016-11-21 5 views

Antwort

3

Es macht nichts ...

Die DOMParser in xmldom fand auch XML

See erstellen: xmldom

https://www.npmjs.com/package/xmldom

const DOMParser = require('xmldom').DOMParser; 
const XMLSerializer = require('xmldom').XMLSerializer; 

// Append a child element 
function appendChild(xmlDoc, parentElement, name, text) { 
    let childElement = xmlDoc.createElement(name); 
    if (typeof text !== 'undefined') { 
    let textNode = xmlDoc.createTextNode(text); 
    childElement.appendChild(textNode); 
    } 
    parentElement.appendChild(childElement); 
    return childElement; 
} 

export function exportToXmlDoc(myPlan) { 

    // documentElement always represents the root node 
    const xmlDoc = new DOMParser().parseFromString("<doc></doc>"); 
    const rootElement = xmlDoc.documentElement; 
    const folderElement = appendChild(xmlDoc, rootElement, 'folder'); 
    appendChild(xmlDoc, folderElement, 'version', 0); 
    const planElement = appendChild(xmlDoc, folderElement, 'plan'); 
    appendChild(xmlDoc, planElement, 'name', 'Eddie'); 

    const xmlSerializer = new XMLSerializer(); 
    const xmlOutput = xmlSerializer.serializeToString(xmlDoc); 

    console.log("xmlOutput:", xmlOutput); 

    return xmlOutput; 
} 

xmloutput :

<doc> 
    <folder> 
     <version>0</version> 
     <plan> 
       <name>Eddie</name> 
     </plan> 
    </folder> 
</doc> 
Verwandte Themen