xml-edit
Modify text-nodes in XML files
Last updated 3 years ago by ivarni .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install xml-edit 
SYNC missed versions from official npm registry.

xml-edit

wat

Parse an XML file to JSON object, modify text nodes and write it to a stream

why

I found myself needing to update text nodes in a maven pom from a node project

how

Given a file that looks like this

<this>
    <is>
        <another>
            XML file
        </another>
    </is>
</this>
const path = require('path');
const xmlEdit = require('xml-edit');

const editor = xmlEdit.getInstance({
    indentation: 2,
    strict: true,
});

const xmlObject = await editor.read(path.resolve('test.xml'));
/*
    {
      "this": {
        "is": {
          "another": "XML file"
        }
      }
    }
*/
xmlObject.this.is.another = 'ohai';
await editor.write(path.resolve('test.xml'), xmlObject, process.stdout);

but...?

getInstance(opts)

opts.indentation: number of spaces to use when writing (default: 2)

opts.strict: parse XML strictly or not (default: true)

instance.read(filePath)

filePath: Path to the file (doh)

instance.write(filePath, xmlObject, stream)

filePath: Path to the original file

xmlObject: Specification of which text nodes to change

stream: A writeable stream that the result gets sent to

ok, so...?

  • Original indentation in the file is not respected
  • You can't edit attributes, in fact they don't even get returned by read
  • You can't edit tagnames
  • Your attributes will be inlined in the tag in the result

This has not been tested very thoroughly so there are probably bugs. Let me know and I'll look at it.

For now consider this a beta at best.

This module sucks

Don't use it then :)

Or file a bug and/or open a PR.

Current Tags

  • 1.0.2                                ...           latest (3 years ago)

3 Versions

  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (2)
Dev Dependencies (17)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |