2016-04-14 17 views
0

"XML" değerinden niçin "value" etiketinin içinde, nodejs dizisindeki bir dizide nasıl ayrışırım? Npm xpath/xmldom, XMLExtract sonra xml2json ama henüz başarılı olmadan denedim.XML dizisinde array

<response status="success"><result> 
    <resource-monitor> 
    <data-processors> 
     <dp0> 
     <second> 
      <cpu-load-average> 
      <entry> 
       <coreid>0</coreid> 
       <value>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</value> 
      </entry> 
      <entry> 
       <coreid>1</coreid> 
       <value>2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 
,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2</value> 
      </entry> 

cevap

1

Seçeneklerden biri böyle cheerio kullanmaktır:

var cheerio = require('cheerio'); 
var _ = require('lodash'); 

var $ = cheerio.load('<cpu-load-average>\ 
     <entry>\ 
      <coreid>0</coreid>\ 
      <value>0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</value>\ 
     </entry>\ 
     <entry>\ 
      <coreid>1</coreid>\ 
      <value>2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2</value>\ 
     </entry>\ 
    </cpu-load-average>', 
    { xmlMode: true } 
); 

var values = _.map($('cpu-load-average').find('value'), function(item) { 
    return $(item).text().split(','); 
}); 

console.log(values); 

requirebin Deneyin. Büyük bir XML dosyası ise, cheerio xml2js'dan daha hızlı olmalıdır.

1

Sen xml2js kullanarak ve daha sonra değer dizisi üretecek string split operation kullanılarak value etiketinin değerini çatlıyor json için xml dönüştürebilirsiniz.