简体   繁体   English

lazy.js和xml流解析

[英]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. 我正在尝试使用xml流模块懒惰地解析kml文件,并且缺少相关的示例。 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. 因此,我们的想法是通过过滤endElement事件上事件发射器的输出来复制注释掉的文本的行为。 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. 我是流媒体和lazy.js的新手,如果这是一个完全的noob问题,那么道歉。 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 . 所以,昨天我发布了Lazy.js的0.3.2版,其中包含一个名为createWrapper的方法。 From the docs: 来自文档:

Defines a wrapper for custom StreamLikeSequences. 定义自定义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). 如果您想要一种将事件流作为序列处理的方法,这很有用,但您不能使用Lazy的现有接口(即,您使用自己的自定义事件从库中包装对象)。

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. 这只是最终可能在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): 但是,正如目前存在的那样,这里有一个例子,说明如何使用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);
});

Output: 输出:

Tessellated
Untessellated
Absolute
Absolute Extruded
Relative

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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