vvar readline = require('readline');
var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
var fs = require('fs');
var root;
var rl = readline.createInterface
({
input: process.stdin,
output: process.stdout
});
function createXML(root)
{
var etree = new ElementTree(root);
rl.question('\nEnter file name to create xml: \n', function (f){
var xml = etree.write({'xml_declaration': false});
console.log(xml);
var newfile = fs.writeFileSync(f,xml);
console.log("\nXML file created..!!\n");
});
}
function runElement(subroot,num1){
if(num1 > 0)
{
rl.question('\nEnter element name: \n', function(name1){
rl.question('\nEnter element data: \n', function (data)
{
var subroot2 = subElement(subroot, name1);
subroot2.text = data;
num1 = num1 - 1;
runElement(subroot,num1);
});
});
}
else
{
createXML(root);
}
}
function getSubRootElements(root, numItem)
{
if(numItem > 0){
rl.question('\nEnter sub root element name: \n', function (name){
var subroot = subElement(root,name);
rl.question('\nEnter the number of elements: \n', function(num1){
runElement(subroot,num1);
});
});
}
}
function start()
{
rl.question('\nEnter root element: \n', function(x){
root = element(x);
rl.question('\nEnter the number of sub root elements: \n', function(numItem){
getSubRootElements(root, numItem);
});
});
}
start();
上面是用于在节点js中创建xml的代码。我试图创建多个子根和元素,但我没有得到我被卡住的地方。我从控制台给出输入,因此无法在循环中编写函数。应该这样做吗?
答案 0 :(得分:-2)
试试这个:
var readline = require('readline');
var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
var fs = require('fs');
var root;
var rl = readline.createInterface
({
input: process.stdin,
output: process.stdout
});
function createXML(root)
{
var etree = new ElementTree(root);
rl.question('\nEnter file name to create xml: \n', function (f){
var xml = etree.write({'xml_declaration': false});
console.log(xml);
var newfile = fs.writeFileSync(f,xml);
console.log("\nXML file created..!!\n");
});
}
function getElements(root, subRoot, numSubRoot, numElements)
{
if(numElements > 0)
{
rl.question('\nEnter element name: \n', function(name1)
{
rl.question('\nEnter element data: \n', function (data)
{
var subroot2 = subElement(subRoot, name1);
subroot2.text = data;
numElements = numElements - 1;
getElements(root, subRoot, numSubRoot, numElements);
});
});
}
else
{
//get the next subroot element
getSubRootElements(root, numSubRoot)
}
}
function getSubRootElements(root, numSubRoot)
{
if(numSubRoot > 0)
{
rl.question('\nEnter sub root element name: \n', function (SubRootName)
{
var subRoot = subElement(root, SubRootName);
rl.question('\nEnter the number of elements: \n', function(numElements)
{
numSubRoot = numSubRoot - 1;
getElements(root, subRoot, numSubRoot, numElements);
});
});
}
else
{
createXML(root);
}
}
function start()
{
rl.question('\nEnter root element: \n', function(x)
{
root = element(x);
rl.question('\nEnter the number of sub root elements: \n', function(numSubRoot)
{
getSubRootElements(root, numSubRoot);
});
});
}
start();