XML: read and write with Node.js

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • blog.johnnyreilly.com

    This is the source code for https://johnnyreilly.com

    import { XMLParser, XMLBuilder } from 'fast-xml-parser'; import fs from 'fs'; import path from 'path'; interface Sitemap { urlset: { url: { loc: string; changefreq: string; priority: number }[]; }; } async function trimXML() { const sitemapPath = path.resolve( '..', 'blog-website', 'build', 'sitemap.xml' ); console.log(`Loading ${sitemapPath}`); const sitemapXml = await fs.promises.readFile(sitemapPath, 'utf8'); const parser = new XMLParser({ ignoreAttributes: false, }); let sitemap: Sitemap = parser.parse(sitemapXml); const rootUrl = 'https://blog.johnnyreilly.com'; const filteredUrls = sitemap.urlset.url.filter( (url) => url.loc !== `${rootUrl}/tags` && !url.loc.startsWith(rootUrl + '/tags/') && !url.loc.startsWith(rootUrl + '/page/') ); console.log( `Reducing ${sitemap.urlset.url.length} urls to ${filteredUrls.length} urls` ); sitemap.urlset.url = filteredUrls; const builder = new XMLBuilder({ format: false, ignoreAttributes: false }); const shorterSitemapXml = builder.build(sitemap); console.log(`Saving ${sitemapPath}`); await fs.promises.writeFile(sitemapPath, shorterSitemapXml); } trimXML();

  • fast-xml-parser

    Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.

    After experimenting with a few different XML parsers I settled on fast-xml-parser. It's fast, it's simple and it's well maintained. It also handles XML namespaces and attributes well. (This appears to be rare in XML parsers.)

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts