npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ncbijs/xml

v0.1.1

Published

Zero-dependency regex-based XML reader for NCBI document formats

Readme

Runtime: Browser + Node.js


Why

DOM parsers like DOMParser or xml2js are heavy dependencies that need a runtime or polyfills. NCBI XML responses have predictable, well-documented structure — a targeted regex reader is faster, lighter, and works everywhere.

@ncbijs/xml extracts tags, blocks, and attributes from NCBI XML without external dependencies or platform assumptions.

  • Tag extraction — text content of leaf elements
  • Block extraction — full inner content including nested tags, with correct handling of same-name nesting
  • Attribute reading — attribute values from opening tags, with entity decoding
  • Entity decoding&, <, >, ", ', and numeric entities (&#x...;, &#...;)
  • Tag stripping — remove all XML tags and decode entities in one call

Install

npm install @ncbijs/xml

Quick start

import { readTag, readBlock, readAllTags, stripTags } from '@ncbijs/xml';

const xml = `
<PubmedArticle>
  <MedlineCitation>
    <PMID>12345678</PMID>
    <Article>
      <ArticleTitle>A <i>novel</i> approach</ArticleTitle>
      <Language>eng</Language>
    </Article>
  </MedlineCitation>
</PubmedArticle>`;

readTag(xml, 'PMID');
// => '12345678'

readBlock(xml, 'ArticleTitle');
// => 'A <i>novel</i> approach'

stripTags('A <i>novel</i> approach');
// => 'A novel approach'

readAllTags(xml, 'Language');
// => ['eng']

API

readTag(xml, tagName)

Extract the text content of the first matching tag. Only captures text between the open and close tags — no nested elements.

readTag('<PMID Version="1">12345678</PMID>', 'PMID');
// => '12345678'

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns string | undefined.

readAllTags(xml, tagName)

Extract text content of all matching tags.

readAllTags('<Keyword>cancer</Keyword><Keyword>genomics</Keyword>', 'Keyword');
// => ['cancer', 'genomics']

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns ReadonlyArray<string>.

readBlock(xml, tagName)

Extract the full inner content (including nested tags) between the first matching open/close pair. Handles nested same-name tags correctly.

readBlock('<Abstract><p>First.</p><p>Second.</p></Abstract>', 'Abstract');
// => '<p>First.</p><p>Second.</p>'

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns string | undefined.

readAllBlocks(xml, tagName)

Extract inner content of all matching blocks.

readAllBlocks('<sec><p>A</p></sec><sec><p>B</p></sec>', 'sec');
// => ['<p>A</p>', '<p>B</p>']

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns ReadonlyArray<string>.

readAttribute(xml, tagName, attrName)

Extract the value of an attribute from the first matching tag.

readAttribute('<PMID Version="1">12345678</PMID>', 'PMID', 'Version');
// => '1'

| Parameter | Type | Description | | ---------- | -------- | ----------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. | | attrName | string | Attribute name to read. |

Returns string | undefined.

readTagWithAttributes(xml, tagName)

Extract text content and all attributes from the first matching tag.

readTagWithAttributes('<Keyword MajorTopicYN="Y">cancer</Keyword>', 'Keyword');
// => { text: 'cancer', attributes: { MajorTopicYN: 'Y' } }

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns TagWithAttributes | null.

readAllTagsWithAttributes(xml, tagName)

Extract text content and attributes from all matching tags.

readAllTagsWithAttributes(
  '<DescriptorName UI="D009369" MajorTopicYN="Y">Neoplasms</DescriptorName>',
  'DescriptorName',
);
// => [{ text: 'Neoplasms', attributes: { UI: 'D009369', MajorTopicYN: 'Y' } }]

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns ReadonlyArray<TagWithAttributes>.

readAllBlocksWithAttributes(xml, tagName)

Extract inner content and attributes from all matching blocks.

readAllBlocksWithAttributes(
  '<article-id pub-id-type="doi">10.1234/example</article-id>',
  'article-id',
);
// => [{ content: '10.1234/example', attributes: { 'pub-id-type': 'doi' } }]

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to search. | | tagName | string | Tag name to find. |

Returns ReadonlyArray<BlockWithAttributes>.

stripTags(xml)

Remove all XML tags from a string.

stripTags('<p>A <b>bold</b> statement</p>');
// => 'A bold statement'

| Parameter | Type | Description | | --------- | -------- | -------------------- | | xml | string | XML string to strip. |

Returns string.

removeAllBlocks(xml, tagName)

Remove all occurrences of a block (open tag through close tag, including content). Also removes self-closing elements.

removeAllBlocks('<body><xref>1</xref> text</body>', 'xref');
// => '<body> text</body>'

| Parameter | Type | Description | | --------- | -------- | --------------------- | | xml | string | XML string to modify. | | tagName | string | Tag name to remove. |

Returns string.

decodeEntities(text)

Decode XML entities: &amp;, &lt;, &gt;, &quot;, &apos;, and numeric character references (&#123;, &#x1F4A1;).

decodeEntities('Smith &amp; Jones &#x2014; 2024');
// => 'Smith & Jones — 2024'

| Parameter | Type | Description | | --------- | -------- | --------------- | | text | string | Text to decode. |

Returns string.

Types

All types are exported for use in your own interfaces:

import type { TagWithAttributes, BlockWithAttributes } from '@ncbijs/xml';