시작하기
StAX-XML은 pure JavaScript parser core로 모든 JavaScript 런타임에서 작동하는 JavaScript/TypeScript용 고성능 풀 기반 XML 파서입니다.
원하시는 패키지 매니저를 사용하여 StAX-XML을 설치하세요:
# npmnpm install stax-xml
# yarnyarn add stax-xml
# pnpmpnpm add stax-xml
# bunbun add stax-xmlESM-only 패키지: StAX-XML은 ESM-only로 배포됩니다. require('stax-xml') 대신 import { ... } from 'stax-xml'를 사용하세요.
브라우저 CDN
섹션 제목: “브라우저 CDN”번들러 없이 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은 두 가지 주요 파싱 방법을 제공합니다:
비동기 파싱 (EventReader)
섹션 제목: “비동기 파싱 (EventReader)”스트림을 사용한 대용량 XML 파일의 메모리 효율적인 처리:
import { EventReader } from 'stax-xml';
const reader = new EventReader(readableStream);for await (const event of reader) { // XML 이벤트 처리}동기 파싱 (EventReaderSync)
섹션 제목: “동기 파싱 (EventReaderSync)”작은 인메모리 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를 사용합니다.