我正在尝试使用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问题,请道歉。也许我只是不理解我从循环中得到的对象。
答案 0 :(得分:2)
所以,昨天我发布了Lazy.js的0.3.2版本,其中包含一个名为createWrapper
的方法。来自文档:
定义自定义StreamLikeSequences的包装器。如果,这很有用 你想要一种方法来处理事件流作为一个序列,但你 不能使用Lazy的现有界面(即,你正在包装一个对象 来自具有自己的自定义事件的图书馆。)
不要指望这种方法以这种确切的形式(甚至是这个确切的名称)无限期地存在;这只是最终可能在Lazy 1.0中最终结果的初步草图。但是,正如目前所存在的,这里有一个示例,说明如何使用来自Google first example KML file的KML tutorial {{3}}来使用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