A lightweight and fully type-safe XML parser factory for custom-tagged XML formats like CUSTOM_ML.
Allows you to define your own valid tag set, parse the XML into a tree structure, and perform tag-safe type filtering.
npm install any-xml-parserOr with Yarn:
yarn add any-xml-parser- 🍿 User-defined tag set (no hardcoded
<div>,<body>nonsense) - 🔒 Type-safe tag filtering with
isTargetTag - 📦 Fully self-contained (no dependencies)
- 🫖 Ideal for design tools, rich XML formats, and offline parsing
import { createXMLParser } from "any-xml-parser";
const parser = createXMLParser(["anyml", "page", "backgroundimage", "textbox", "picturebox"] as const);const xml = `
<anyml>
<page name="cover">
<textbox text="Hello, world!" />
</page>
</anyml>
`;
const result = parser.parseXML(xml);
console.dir(result, { depth: null });if (parser.isTargetTag("page")) {
// ✅ type is narrowed to "anyml" | "page" | ...
}
console.log(parser.TAGS.PAGE); // → "page"
console.log(parser.TARGET_TAGS.has("textbox")); // truetype XmlNode = {
type: "root" | "element" | "text";
attr?: { [key: string]: string };
tag?: string;
content?: string;
children?: XmlNode[];
};
type TagValue = typeof parser.type.TagValue; // "anyml" | "page" | ...You can create and export a parser instance:
// xml-parser.ts
export const xmlParser = createXMLParser([...yourTags] as const);
export type TagValue = typeof xmlParser.type.TagValue;Then use it globally:
import { xmlParser } from "./xml-parser";
xmlParser.parseXML(...);MIT © 2025 anveloper