var readline = require('readline');
var fs = require('fs');
var os = require('os');
var fReadName = './Loclistcn.xml';//源文件
var fWriteName = './LocListcn1.xml';//目标文件
var fRead = fs.createReadStream(fReadName);
var fWrite = fs.createWriteStream(fWriteName);
var enableWriteIndex = true;
var enableWriteIndex1 = true;
fRead.on('end', ()=>{
console.log('end');
enableWriteIndex = false;
});
var index = 1;
var arr = [];
var code = ""
var country = ""
var objReadline = readline.createInterface({
input: fRead,
output: fWrite,
/*terminal: true*/
});
objReadline.on('line', (line)=>{
//正则处理
var rg1 = /(<State)(.*)(Code=")([a-zA-Z 0-9]*)(">)/
var rg3 = /(<State)(.*)(>)/
var rg4 = /(<CountryRegion)(.*)(Code=")([a-zA-Z 0-9]*)(">)/
var rg2 = /(<City.*)(Code=")([a-zA-Z 0-9]*)((".*\/>)|(">))/
var rg6 = /(<\/City.*)(>)/
var rg5 = /( <Region .*)(Code=")([a-zA-Z 0-9]*)(".*)(\/>)/
var str1
if(rg1.test(line)){
code =line.replace(rg1,RegExp.$4).trim()
}else if(rg3.test(line)){
code = ""
}
if(rg4.test(line)){
country =line.replace(rg4,RegExp.$4).trim()
}
if(rg2.test(line)){
str1 =line.replace(rg2,RegExp.$1 + ' cityCode="'+ (code ? (code+'-') : code)+ RegExp.$3+ '"' +
' countryCode="'+ country + '" ' +RegExp.$2 + RegExp.$3 +(RegExp.$4 ==='">' ? ' "/>' : RegExp.$4 ))
}else{
str1 = line
}
if(!rg5.test(line) && !rg6.test(line)){
var tmp =str1 + '\n';
// console.log(tmp)
str1 && fWrite.write(tmp);
}
index ++
});
objReadline.on('close', ()=>{
console.log('readline close...');
});
网友评论