콘텐츠로 이동
StAX-XML

StreamReaderSync - 동기 current-token XML 파싱

StreamReaderSync는 string, Uint8Array, 또는 Iterable<Uint8Array>를 받는 동기 current-token reader입니다.

import { StreamReaderSync, XmlEventType } from 'stax-xml';
const reader = new StreamReaderSync('<catalog><book id="b1">StAX</book></catalog>');
try {
while (reader.next() !== null) {
if (reader.eventType() === XmlEventType.START_ELEMENT) {
console.log(reader.name(), reader.attributeValue('id'));
}
}
} finally {
reader.close();
}

String input은 byte로 encoding하지 않고 직접 scan합니다. Byte input은 fatal TextDecoder로 incremental decoding합니다. encoding 기본값은 utf-8이며 host decoder가 지원하는 utf-16le, utf-16be 등의 label을 지정할 수 있습니다. XML declaration에서 label을 자동 추론하지 않으므로 byte source와 일치시켜야 합니다.

interface StreamReaderSyncOptions {
documentMode?: 'document' | 'fragment';
namespaceAware?: boolean; // 기본값: true
autoDecodeEntities?: boolean; // 기본값: true
addEntities?: { entity: string; value: string }[];
encoding?: string; // byte input 전용, 기본값: 'utf-8'
}

namespaceAware의 기본값은 true입니다. raw qualified name만 필요하면 false로 설정하세요. namespace URI는 ''가 되고, xmlns 선언은 일반 attribute로 노출되며, 선언되지 않은 prefix도 거부하지 않습니다.

eventType(), name(), text(), localName(), prefix(), namespaceURI(), attribute metadata, attributeValue(), namespaceURIForPrefix()를 제공합니다. Accessor는 current token만 설명합니다. autoDecodeEntities 기본값은 true이며 predefined, numeric, configured custom entity를 single-pass decode합니다. false이면 validation을 유지하면서 raw reference 표기를 반환합니다. CDATA는 항상 literal입니다. addEntities는 DTD processing 없이 trusted, non-recursive internal definition을 제공합니다. External entity resolution과 외부 I/O는 수행하지 않습니다.

중단할 때는 reader.close()를 호출하세요. close는 idempotent이며 underlying byte iterator를 닫습니다. stable event object가 필요하면 EventReaderSync를 사용하세요.