2014-01-06 1 views
0

XML 스트림 모듈과 함께 느리게 kml 파일을 구문 분석하고 관련 예제가 부족한 상태로 실행하려고합니다. 지금까지 내 코드가 있습니다.lazy.js 및 xml 스트림 구문 분석

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에 새로운 사람들입니다. 이것이 총 멍청한 질문 인 경우 사과드립니다. 아마도 루프에서 벗어나는 객체를 이해하지 못했을 것입니다.

+0

은'XML-stream' 라이브러리가 약간 다른 인터페이스를 제공 뭔가 * 스트림과 같은 *을 노출 한 것으로 보여이 하나가 까다 롭다 당신의 일상적인'stream.Readable' 노드에. Lazy는 현재 시퀀스로 임의의 소스를 래핑하는 방법을 제공하지 않습니다. 그러나 그것은 가까운 장래에 추가 할 부분입니다. 나는이 질문이있을 때 다시 살펴 보겠다. –

+0

응답 해 주셔서 감사합니다. 그래서 지금 내 옵션은 json 예제에서와 같이 xml을 구문 분석하는 사용자 정의 시퀀스를 작성하거나 stream.Readable 인터페이스를보다 엄격하게 준수하는 XML 스트림 모듈을 작성하는 것입니다. 후자의 경로로 가면 lazy의 각 출력이 on.data 이벤트와 같을 것이라고 예상해야합니다. 맞습니까? – ddombrow

답변

2

어제 저는 이라는 메서드가 포함 된 Lazy.js의 0.3.2 버전을 게시했습니다. 문서에서 :

사용자 지정 StreamLikeSequences에 대한 래퍼를 정의합니다. 이는 이벤트 시퀀스를 시퀀스로 처리하려는 경우에 유용하지만, 은 지연 객체의 기존 인터페이스를 사용할 수 없습니다. 즉, 사용자 정의 이벤트가있는 라이브러리에서 객체를 래핑합니다.

정확하게이 형식으로 (또는 정확한 이름으로도)이 방법을 사용하면 안됩니다. 그것은 Lazy 1.0에서 궁극적으로 끝날지 모르는 것들에 대한 예비 스케치 일뿐입니다. 하지만 현재 존재하는 것처럼 여기에 Google KML tutorialfirst example KML file을 사용하여 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 
+0

니스! 곧 이걸 시도 할거야. 나는 이것이 추가를위한 좋은 개념이 될 것이라고 생각한다. npm 모듈을 연구하면서, 저는 파서 (parser)와 같이 스트림과 같은 "패턴"을 꽤 많이 알아 봤습니다. – ddombrow