0

此请求返回一个压缩文件,其中包含一个 XML 文件。

myXMLText = ''
request({
    url: "http://webservice.newrastreamentoonline.com.br/",
    method: "POST",
    headers: {
        "content-type": "application/xml",  // <--Very important!!!
    },
    body: myXMLText
    }, function (error, response, body){
    //console.log(body);
})
.pipe(fs.createWriteStream('rastreamento.zip'))

我想打开 zip 文件,读取 XML 文件,并将内容解析到 XML 变量上的这个函数

var result = convert.xml2js(xml,{alwaysChildren: true}); 

我该怎么做?

4

1 回答 1

0

努力工作。

必须解压缩文件,阅读它,它可以工作。

我认为有一个更好的解决方案,但现在,它有效

request({
    url: "http://webservice.xxxxxxxxx.com.br/",
    method: "POST",
    headers: {
        "content-type": "application/xml",  // <--Very important!!!
    },
    body: myXMLText
    }, function (error, response, body){
    
    })
.pipe(fs.createWriteStream('onix.zip')) //salvo o arquivo zip
.on('finish',()=>{
    console.log('Arquivo baixado do WebService')
    fs.createReadStream('onix.zip')
    .pipe(unzipper.Parse())
    .on('entry', function (entry) {
      console.log('Descompactando arquivo')
      const fileName = entry.path;
      const type = entry.type; // 'Directory' or 'File'
      const size = entry.vars.uncompressedSize; // There is also compressedSize;
      console.log(fileName)
      entry.pipe(fs.createWriteStream('output/onix.xml')
                .on('error', err => console.log('error', err.message))
                .on('ready',()=>{
                    console.log('Lendo arquivo')
                    fs.createReadStream('output/onix.xml')
                    .on('data', function (chunk) { 
                        //console.log('aqui4')
                        xml+=chunk.toString()
                        //console.log(xml); 
                    })
                    .on('end', function (chunk) {
                        //console.log(xml) 
                        console.log('Convertendo de XML p/ JSON')
                        result = convert.xml2js(xml,{alwaysChildren: true}); // or convert.xml2json(xml, options)\
                        
                        console.log('Preparando SQL STATMENT | '+result.elements[0].elements.length+' LINHAS')
                        sql='';
                        for(i=0;i<result.elements[0].elements.length;i++){//trocar o 5 por result.elements[0].elements.length
                            table=''
                            value=''
                            for(j=0;j<result.elements[0].elements[i].elements.length;j++){
                                //sql += (result.elements[0].elements[i].elements[j].name);
                                //console.log(JSON.parse(result.elements[0].elements[i].elements[j].elements));                        
                                try{
                                    if(result.elements[0].elements[i].elements[j].name.toLowerCase()=='rod' || result.elements[0].elements[i].elements[j].elements[0].text==null){
                        
                                    }else{
                                        table += result.elements[0].elements[i].elements[j].name.toLowerCase()+(j==(result.elements[0].elements[i].elements.length-1)?'':',')
                                        value += "'"+result.elements[0].elements[i].elements[j].elements[0].text.replace('\'',' ').replace('true','1').replace('false','0')+"'"+(j==(result.elements[0].elements[i].elements.length-1)?'':',')
                                    }                                    
                                }catch{
                        
                                }
                            }
                            sql += 'INSERT INTO onixsat (' + table + ') VALUES (' + value + ')'
                            sql+=';\n'
                        }
                        console.log('Executando SQL')
                        //console.log(sql)
                        
                    })
                })
                
            )
    })
})
于 2020-08-16T01:21:18.503 回答