1

I am trying to parse very large XML files with xml-stream in a nodejs script.

xml-stream can be found here - 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>

And my nodejs code looks like this...

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);
});

The output...

{ '$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' }

How do I get the image URLs?

I have tried .collect() and .preserve() on various nodes in various orders. There doesn't seem to be a lot of more complicated usage examples for this lib. I have very large XML files, and xml2js couldn't handle it. I would be happy with this lib choice if I could figure out how to increase the depth in some fashion.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kirby
  • 1,980
  • 3
  • 21
  • 33

1 Answers1

1

If you just want to get the URLs

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));
})

Output:

{
  "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"
      }
    ]
  }
}
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • This is much closer! there is only one MediaUrl, though. – Kirby Jan 12 '17 at 04:02
  • ah great! The problem was the print to console! Ah man! Well at least this will provide another example on the internet for xml-stream. Thank you. – Kirby Jan 12 '17 at 04:14