几点问题:
1、怎么直接添加子节点
2、判断目录有效性
3、parseString有没有同步接口
4、readFileSync有没有接口判断读文件是否成功
const fs = require("fs"); const xml2js = require('xml2js'); const path = require("path") // var xmlParser = new xml2js.Parser({explicitArray:false}); const xmlParser = new xml2js.Parser(); const languageDir = 'D:\\CODE\\testnodejs\\languages'; const outDir = 'D:\\CODE\\testnodejs'; const outxmlName = 'newstring.xml'; var xmlString = '<?xml version="1.0" encoding="UTF-8"?>' +'<resources xmlns:tools="http://schemas.android.com/tools" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">' +'<string></string>' +'</resources>' xmlParser.parseString(xmlString,function(err,allResult) { var allstrings = allResult.resources.string; var files = fs.readdirSync(languageDir); files.forEach(file => { var data = fs.readFileSync(path.join(languageDir, file + "\\string.xml")); // var data = fs.readFileSync("./languages/values/string.xml"); xmlParser.parseString(data,function(err,result){ var strings = result.resources.string; strings.forEach(string => { string.$.filename = file; allstrings.push(string); }) }); }); delete allstrings[0]; const builder = new xml2js.Builder(); const xml = builder.buildObject(allResult); fs.writeFile(path.join(outDir, outxmlName), xml, (error) => { if (error) { throw error; } console.log("write file OK"); }); }) console.log("do job success");
标签:xml,fs,const,string,nodejs,var,xml2js From: https://www.cnblogs.com/SaraMoring/p/16643183.html