콘텐츠로 이동
StAX-XML

시작하기

StAX-XML은 pure JavaScript parser core로 모든 JavaScript 런타임에서 작동하는 JavaScript/TypeScript용 고성능 풀 기반 XML 파서입니다.

원하시는 패키지 매니저를 사용하여 StAX-XML을 설치하세요:

Terminal window
# npm
npm install stax-xml
# yarn
yarn add stax-xml
# pnpm
pnpm add stax-xml
# bun
bun add stax-xml

ESM-only 패키지: StAX-XML은 ESM-only로 배포됩니다. require('stax-xml') 대신 import { ... } from 'stax-xml'를 사용하세요.

번들러 없이 jsDelivr의 ESM 번들을 바로 불러다 쓸 수도 있습니다.

<script type="module">
import {
EventReaderSync,
XmlEventType,
} from 'https://cdn.jsdelivr.net/npm/stax-xml@latest/dist/index.js';
for (const event of new EventReaderSync('<root><item>ok</item></root>')) {
if (event.type === XmlEventType.START_ELEMENT) console.log(event.name);
}
</script>

jsDelivr 컨버터 번들(https://cdn.jsdelivr.net/npm/stax-xml@latest/dist/converter.js)과 unpkg(https://unpkg.com/stax-xml@latest/dist/index.js, https://unpkg.com/stax-xml@latest/dist/converter.js)도 같은 방식으로 쓸 수 있습니다. 모두 ESM 전용 URL이므로 일반 <script src>로는 동작하지 않으며, 프로덕션에서는 @latest 대신 특정 버전(예: stax-xml@<version>)으로 고정해 두는 편이 안전합니다.

StAX-XML은 웹 표준 API만을 사용하여 다음과 호환됩니다:

  • Node.js (v20.19+)
  • Bun (모든 버전)
  • 웹 브라우저 (최신 브라우저)
  • 엣지 런타임 (Vercel, Cloudflare Workers 등)

StAX-XML은 두 가지 주요 파싱 방법을 제공합니다:

스트림을 사용한 대용량 XML 파일의 메모리 효율적인 처리:

import { EventReader } from 'stax-xml';
const reader = new EventReader(readableStream);
for await (const event of reader) {
// XML 이벤트 처리
}

작은 인메모리 XML 문자열의 고성능 파싱:

import { EventReaderSync } from 'stax-xml';
const reader = new EventReaderSync(xmlString);
for (const event of reader) {
// XML 이벤트 처리
}

알려진 XML-to-object output에는 stax-xml/converter를 우선 사용하세요. Allocation-sensitive traversal에는 동기 string/byte input용 StreamReaderSync 또는 비동기 byte input용 StreamReader를 사용합니다.