# karma-xml2js-preprocessor

> A Karma plugin. Convert XML files into JavaScript DOM objects to serve them in a script tag.

Latest version **0.2.0** (published 2026-07-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install karma-xml2js-preprocessor
pnpm add karma-xml2js-preprocessor
yarn add karma-xml2js-preprocessor
bun add karma-xml2js-preprocessor
```

## Health

**Score 50/100 (C)** — status: active.

Positive: no vulnerabilities; recently updated.

Warnings: low downloads; no types; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2026-07-02 |
| First published | 2021-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | PhonicScore GmbH |
| Maintainers | sschmidtu |

## Links

- npm: https://www.npmjs.com/package/karma-xml2js-preprocessor
- Repository: https://github.com/opensheetmusicdisplay/karma-xml2js-preprocessor
- Homepage: https://github.com/opensheetmusicdisplay/karma-xml2js-preprocessor#readme
- Issues: https://github.com/opensheetmusicdisplay/karma-xml2js-preprocessor/issues
- npm.io page: https://npm.io/package/karma-xml2js-preprocessor

## Recent versions

- 0.2.0 (latest) — 2026-07-02
- 0.1.0 — 2021-08-30

## README

# karma-xml2js-preprocessor

Preprocessor for converting XML files into JavaScript DOM objects.

## Installation

Just run:
```bash
npm install karma-xml2js-preprocessor --save-dev
```

## Configuration
This is the default configuration:
```js
// karma.conf.js
module.exports = function(config) {
  config.set({
    preprocessors: {
      '**/*.xml': ['xml2js']
    },
    files: [
      '**/*.xml',
      '*.js'
    ]
  });
};
```

## How does it work ?

This preprocessor converts XML files into JS DOM objects and publishes them in the global `window.__xml__`.

### Encoding / BOM support

Karma decodes file content as UTF-8 before handing it to preprocessors, which garbles files saved
as UTF-16 (e.g. MusicXML exported by Finale or Sibelius) and produces an invalid generated `.js`.
This preprocessor detects that case (UTF-16 content shows up riddled with NUL characters, which are
illegal in XML), re-reads the raw bytes from disk, and decodes them according to the byte order mark
(BOM): UTF-16 LE, UTF-16 BE, and UTF-8 with BOM are all handled, and any leading BOM is stripped so
the document starts cleanly with `<?xml`. Plain UTF-8 files are unaffected.

For instance this `doc.xml`...
```xml
<?xml version="1.0" encoding="UTF-8"?>
<tag>content</tag>
```
... will be served as `doc.xml.js`:
```js
window.__xml__ = window.__xml__ || {};
window.__xml__['doc.xml'] = /* DOM object obtained by parsing the content of doc.xml */;
```

---
_Source: https://npm.io/package/karma-xml2js-preprocessor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
