简体   繁体   中英

lazy.js and xml stream parsing

I'm trying to parse through a kml file lazily with the xml stream module and running into a lack of relevant examples. Here's my code so far.

var fs = require('fs');
var path = require('path');
var xmlStream = require('xml-stream');
var lazy = require('lazy.js')

var stream = fs.createReadStream('./Sindh.kml');

var xml = new xmlStream(stream);

var onlyEvents = function(e) {
    if (e && e._events) {
        return 1;
    }
    else {
        return 0;
    }
}

lazy(xml).filter(onlyEvents).take(20).each(function(e) {
    console.log(e);
    console.log('\n');
});

//xml.preserve('Polygon', true);
//xml.on('endElement: Polygon', function(poly) {
//  var coordString =  poly.outerBoundaryIs.LinearRing.coordinates.$children.join().trim();


  //console.log('\n\n');
//})

So, the idea is to duplicate the behavior of the commented out text by filtering the output from the event emitter on the endElement events. I'm getting output here by running the code, I just don't know quite what I'm looking at or where to go from here.

I'm new to streams and lazy.js so apologies if this is a total noob question. Perhaps I'm just not understanding the objects that I'm getting out from the loop.

So, yesterday I published version 0.3.2 of Lazy.js, which includes a method called createWrapper . From the docs:

Defines a wrapper for custom StreamLikeSequences. This is useful if you want a way to handle a stream of events as a sequence, but you can't use Lazy's existing interface (ie, you're wrapping an object from a library with its own custom events).

Don't count on this method being there in this exact form (or even with this exact name) indefinitely; it's just a preliminary sketch of what might end up ultimately in Lazy 1.0. But as it currently exists, here's an example of how you could use it for your purposes w/ the xml-stream library, using the first example KML file from Google's KML tutorial (I have no idea if this is the "KML" you're using; but it should illustrate how this works regardless):

var fs        = require('fs'),
    XmlStream = require('xml-stream'),
    Lazy      = require('./lazy.node');

// Here we are wrapping an XML stream as defined by xml-stream. We're defining
// our wrapper so that it takes the stream as the first argument, and the
// selector to scan for as the second.
var wrapper = Lazy.createWrapper(function(source, selector) {
  // Within this wrapper function, 'this' is bound to the sequence being created.
  var sequence = this;

  // The xml-stream library emits the event 'endElement:x' when it encounters
  // an <x> element in the document.
  source.on('endElement:' + selector, function(node) {
    // Calling 'emit' makes this data part of the sequence.
    sequence.emit(node);
  });
});

// We can now use the factory we just defined to create a new sequence from an
// XML stream.
var stream = fs.createReadStream('KML_Samples.kml');
var xml = new XmlStream(stream);
var sequence = wrapper(xml, 'Placemark');

// This sequence can be used just like any other, with all of the same helper
// methods we know and love.
sequence.skip(5).take(5).pluck('name').each(function(placemarkName) {
  console.log(placemarkName);
});

Output:

Tessellated
Untessellated
Absolute
Absolute Extruded
Relative

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM