[英]lazy.js and xml stream parsing
我正在尝试使用xml流模块懒惰地解析kml文件,并且缺少相关的示例。 到目前为止,这是我的代码。
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');
//})
因此,我们的想法是通过过滤endElement事件上事件发射器的输出来复制注释掉的文本的行为。 我通过运行代码获得输出,我只是不知道我在看什么或从哪里去。
我是流媒体和lazy.js的新手,如果这是一个完全的noob问题,那么道歉。 也许我只是不理解我从循环中得到的对象。
所以,昨天我发布了Lazy.js的0.3.2版,其中包含一个名为createWrapper
的方法。 来自文档:
定义自定义StreamLikeSequences的包装器。 如果您想要一种将事件流作为序列处理的方法,这很有用,但您不能使用Lazy的现有接口(即,您使用自己的自定义事件从库中包装对象)。
不要指望这种方法以这种确切的形式(甚至是这个确切的名称)无限期地存在; 这只是最终可能在Lazy 1.0中最终结果的初步草图。 但是,正如目前存在的那样,这里有一个例子,说明如何使用Google的KML教程中的第一个示例KML文件将xml-stream
库用于您的目的(我不知道这是否是您的“KML”)重新使用;但它应该说明这是如何工作的):
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);
});
输出:
Tessellated
Untessellated
Absolute
Absolute Extruded
Relative
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.