<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Clickin</title>
    <description>The latest articles on DEV Community by Clickin (@clickin).</description>
    <link>https://dev.to/clickin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3550708%2F63baf7c5-e361-4354-88cf-4696d5b4d408.png</url>
      <title>DEV Community: Clickin</title>
      <link>https://dev.to/clickin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/clickin"/>
    <language>en</language>
    <item>
      <title>Announcing stax-xml: StAX style XML Parser for JavaScript</title>
      <dc:creator>Clickin</dc:creator>
      <pubDate>Tue, 07 Oct 2025 09:22:56 +0000</pubDate>
      <link>https://dev.to/clickin/announcing-stax-xml-stax-style-xml-parser-for-javascript-60k</link>
      <guid>https://dev.to/clickin/announcing-stax-xml-stax-style-xml-parser-for-javascript-60k</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;I built &lt;strong&gt;stax-xml&lt;/strong&gt;, the first StAX-style (streaming) XML parser for JavaScript. It can parse XML files of any size without running into V8's ~1GB string limit that crashes DOM-based parsers. If you work with large XML files, give it a try!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem I Hit
&lt;/h2&gt;

&lt;p&gt;I'm a Java developer. At work, we process large XML files from government agencies and enterprise systems daily. In Java, we use &lt;strong&gt;StAX&lt;/strong&gt; (Streaming API for XML) - a pull-based pattern where you iterate over XML events instead of loading the entire document into memory.&lt;/p&gt;

&lt;p&gt;Last month, I needed to do the same thing in a Node.js project. I searched for a StAX implementation in JavaScript. There wasn't one.&lt;/p&gt;

&lt;p&gt;What I found instead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SAX-based parsers&lt;/strong&gt; (like &lt;strong&gt;xml2js&lt;/strong&gt; which uses sax-js internally): Event-based with callbacks (push-based, not pull-based like StAX)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;XML-to-object mappers&lt;/strong&gt; (like &lt;strong&gt;txml&lt;/strong&gt;, &lt;strong&gt;fast-xml-parser&lt;/strong&gt;): Parse full XML string into JavaScript objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The problem with SAX parsers:&lt;/strong&gt; They use callbacks (push-based), making it difficult to process specific element's text data. You have to maintain state across multiple callback invocations, leading to complex code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem with object mappers:&lt;/strong&gt; They should load the entire XML file into memory as a string first, then build the full object tree. This works great for files under ~100MB, but crashes on large files.&lt;/p&gt;

&lt;p&gt;But when I tried parsing a 900MB government census file, I hit V8's hard limit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RangeError: Invalid string length
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V8 can't create strings larger than ~1GB (2^29 - 1 bytes). No matter which parser I used, they all crashed at the same point because they all rely on loading the full XML string first.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why stax-xml?
&lt;/h2&gt;

&lt;p&gt;I needed a streaming parser that could handle files of any size. In Java, StAX has been the standard since 2004. The pattern is simple: you pull events from the parser (start element, text, end element) and process them one at a time. The parser never loads the full document into memory.&lt;/p&gt;

&lt;p&gt;JavaScript's async model is actually &lt;em&gt;perfect&lt;/em&gt; for this pattern. With &lt;code&gt;ReadableStream&lt;/code&gt; and &lt;code&gt;for await...of&lt;/code&gt;, I could build a fully async StAX implementation that feels natural in modern JavaScript.&lt;/p&gt;

&lt;p&gt;So I built &lt;strong&gt;stax-xml&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pull-based streaming&lt;/strong&gt;: Process XML as events, never load the full document&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No size limits&lt;/strong&gt;: Uses constant ~10MB memory regardless of file size (tested with 2GB+ files)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type-safe&lt;/strong&gt;: Type guards (&lt;code&gt;isStartElement&lt;/code&gt;, &lt;code&gt;isCharacters&lt;/code&gt;) for clean TypeScript&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Converter API&lt;/strong&gt;: Zod-style declarative schemas with XPath selector&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-platform&lt;/strong&gt;: Works in Node.js, Bun, Deno, browsers, and edge runtimes (uses only Web Standard APIs)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Quick Example
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Event-based API&lt;/strong&gt; (low-level streaming):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StaxXmlParser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isStartElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isCharacters&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stax-xml&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StaxXmlParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isStartElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Element: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isCharacters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Text: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Converter API&lt;/strong&gt; (Zod-style schemas):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stax-xml/converter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bookSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;xpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/book/title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;xpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/book/author&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;number&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;xpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/book/price&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bookSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Full TypeScript type inference!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  When to Use It
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;For small files (&amp;lt; 100MB):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;StaxXmlParserSync&lt;/strong&gt; - synchronous, faster for in-memory strings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For medium files (100MB - 900MB):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;StaxXmlParserSync&lt;/strong&gt; for speed, &lt;strong&gt;StaxXmlParser&lt;/strong&gt; for memory efficiency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For large files (900MB+):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Only StaxXmlParser (async)&lt;/strong&gt; - handles unlimited file sizes via streaming&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benchmark (97MB file):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;txml&lt;/strong&gt;: 1.02s, 897.50 MB memory&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StaxXmlParserSync&lt;/strong&gt;: 1.05s, 13.88 MB memory&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StaxXmlParser&lt;/strong&gt;: 3.61s, 3.13 MB memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;stax-xml trades some speed for memory efficiency and unlimited scalability. Perfect for large files or memory-constrained environments (edge functions, containers).&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;I'm actively working on stax-xml and would love feedback. Some areas I'm focusing on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance optimizations for medium-sized files&lt;/li&gt;
&lt;li&gt;Better error messages and debugging tools&lt;/li&gt;
&lt;li&gt;More comprehensive XPath support (currently, XPath spec subset only)&lt;/li&gt;
&lt;li&gt;Additional converter API features&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Try It Out
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;stax-xml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/Clickin/stax-xml" rel="noopener noreferrer"&gt;https://github.com/Clickin/stax-xml&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation&lt;/strong&gt;: &lt;a href="https://clickin.github.io/stax-xml" rel="noopener noreferrer"&gt;https://clickin.github.io/stax-xml&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NPM&lt;/strong&gt;: &lt;a href="https://www.npmjs.com/package/stax-xml" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/stax-xml&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're working with large XML files and hitting memory limits, give stax-xml a try. I'd love to hear your feedback, bug reports, or feature requests!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Built with Web Standard APIs. Works everywhere. No size limits.&lt;/strong&gt; 🚀&lt;/p&gt;

</description>
      <category>node</category>
      <category>xml</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
