Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shun-shobon/littlexml

1kB XML library for Node.js, Bun, Deno, Browser, Edge runtime.
https://github.com/shun-shobon/littlexml

cloudflare-workers deno javascript typescript xml

Last synced: 25 days ago
JSON representation

1kB XML library for Node.js, Bun, Deno, Browser, Edge runtime.

Awesome Lists containing this project

README

        

# @shun-shobon/littlexml

1kB XML library for Node.js, Bun, Deno, Browser, Edge runtime.

[![npm](https://img.shields.io/npm/v/@shun-shobon/littlexml?logo=npm)](https://www.npmjs.com/package/@shun-shobon/littlexml)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@shun-shobon/littlexml)](https://bundlephobia.com/package/@shun-shobon/littlexml)
[![test](https://github.com/shun-shobon/littlexml/actions/workflows/check.yml/badge.svg)](https://github.com/shun-shobon/littlexml/actions/workflows/check.yml)
[![codecov](https://codecov.io/gh/shun-shobon/littlexml/branch/master/graph/badge.svg?token=VAZxHGjjpu)](https://codecov.io/gh/shun-shobon/littlexml)

## About

This library is designed to run on small JavaScript runtimes such as Cloudflare
Workers. It also works on Node.js, Bun, Deno, and browsers.

## Features

- Render XML as a string, iterator, or stream.
- Render XML with indentation.

## Support platforms

- Node.js
- Deno
- Bun
- Browser
- Edge runtime like Cloudflare Workers, Vercel Edge Functions

## Installation

### Node.js / Bun

Install package from npm. You can also use yarn/pnpm instead of npm.
If you are using Bun, you can install with `bun add` command.

```sh
npm install @shun-shobon/littlexml
```

You can import from the package as `@shun-shobon/littlexml`.

```ts
import { element, renderToString } from "@shun-shobon/littlexml";
```

### Deno

You can directly import from `npm:@shun-shobon/littlexml`.

```ts
import { element, renderToString } from "npm:[email protected]";
```

### Browser

You can directly import from `unpkg.com`.

```js
import {
element,
renderToString,
} from "https://unpkg.com/@shun-shobon/[email protected]";
```

## Example

This example is rendering a sitemap.

```typescript
import { element, renderToString } from "@shun-shobon/littlexml";

const root = element("urlset")
.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
.attr("xmlns:image", "http://www.google.com/schemas/sitemap-image/1.1")
.child(
element("url")
.child(element("loc").text("https://example.com/"))
.child(element("lastmod").text("2020-01-01"))
.child(element("changefreq").text("daily"))
.child(element("priority").text("0.8"))
.child(
element("image:image")
.child(element("image:loc").text("https://example.com/image.png"))
.child(element("image:caption").text("caption")),
),
);

const xml = renderToString(root, { version: "1.0", indent: 2 });

console.log(xml);
```

console output:

```xml


https://example.com/
2020-01-01
daily
0.8

https://example.com/image.png
caption

```