2017-01-12 28 views
1

我想在腳本的NodeJS解析的XML流非常大的XML文件的兒童。問題解析XML和收集與XML的流

XML的流可以在這裏找到 - https://github.com/assistunion/xml-stream

<?xml version="1.0"?> 
<Products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en-us" version="0.96" versionTimestamp="2012-02-07T03:00:00Z" fooKey="6402420af51e08"> 
    <Product> 
     <id>296834</id> 
     <name>Thing</name> 
     <Photos> 
      <Photo> 
       <MediaURL>http://url.com/to/image/file</MediaURL> 
      </Photo> 
      <Photo> 
       <MediaURL>http://url.com/to/image/secondfile</MediaURL> 
      </Photo> 
      <Photo> 
       <MediaURL>http://url.com/to/image/thirdfile</MediaURL> 
      </Photo> 
     </Photos> 
    </Product> 
</Products> 

我的代碼的NodeJS看起來像這樣...

var fs  = require('fs') 
, path  = require('path') 
, XmlStream = require('xml-stream') 
; 

// Create a file stream and pass it to XmlStream 
var stream = fs.createReadStream(path.join(__dirname, 'samplekirby.xml')); 
var xml = new XmlStream(stream); 

xml.preserve('Product', true); 
xml.collect('Photos'); 
xml.on('endElement: Product', function(item) { 
    console.log(item); 
}); 

輸出...

{ '$children': 
[ { '$children': [Object], '$text': '296834', '$name': 'id' }, 
{ '$children': [Object], '$text': 'Thing', '$name': 'name' }, 
{ '$children': [Object], Photo: [Object], '$name': 'Photos' } ], 
id: { '$children': [ '296834' ], '$text': '296834', '$name': 'id' }, 
name: { '$children': [ 'Thing' ], '$text': 'Thing', '$name': 'name' }, 
Photos: 
{ '$children': [ [Object], [Object], [Object] ], 
Photo: { '$children': [Object], MediaURL: [Object], '$name': 'Photo' }, 
'$name': 'Photos' }, 
'$name': 'Product' } 

如何我可以獲取圖片網址嗎?

我已經在各種不同的訂單節點上嘗試.collect()和.preserve()。這個庫似乎沒有很多更復雜的用法示例。我有非常大的XML文件,而xml2js無法處理它。如果我能弄清楚如何以某種方式增加深度,我會對這個庫選擇感到高興。

回答

1

如果你只是想獲得的URL

var fs = require('fs'), 
    path = require('path'), 
    XmlStream = require('xml-stream'); 

// Create a file stream and pass it to XmlStream 
var stream = fs.createReadStream(path.join(__dirname, 'sample.xml')); 
var xml = new XmlStream(stream); 

xml.collect('Photo'); 
xml.on('endElement: Product', function(product) { 
    console.log(JSON.stringify(product, null, 2)); 
}) 

輸出:

{ 
    "id": "296834", 
    "name": "Thing", 
    "Photos": { 
    "Photo": [ 
     { 
     "MediaURL": "http://url.com/to/image/file" 
     }, 
     { 
     "MediaURL": "http://url.com/to/image/secondfile" 
     }, 
     { 
     "MediaURL": "http://url.com/to/image/thirdfile" 
     } 
    ] 
    } 
} 
+0

@Kirby更新。 –

+0

這更接近!但只有一個MediaUrl。 – Kirby

+0

@Kirby我的壞。更新。 –