Code Monkey home page Code Monkey logo

to-xml's Introduction

toXML - Pure JavaScript XML Writer

Node.js CI npm version gzip size

Live Demo: https://kawanet.github.io/from-xml/

FEATURES

  • Simple: single writer function toXML() which returns XML string.
  • Small: 2KB minified, 1KB gzipped.
  • Standalone: no external module dependency nor DOM needed.
  • TypeScript definition: to-xml.d.ts

SYNOPSIS

ES Module:

import {toXML} from "to-xml";

Node.js:

const {toXML} = require("to-xml");

Browser:

<script src="https://cdn.jsdelivr.net/npm/to-xml/dist/to-xml.min.js"></script>

Run:

const data = {
  "xml": {
    "@foo": "FOO",
    "bar": {
      "baz": "BAZ"
    }
  }
};

const xml = toXML(data, null, 2);

console.warn(xml);

Result:

<xml foo="FOO">
  <bar>
    <baz>BAZ</baz>
  </bar>
</xml>

EXAMPLES

Empty Element

JavaScript: null

{
  "xml": {
    "foo": {"@bar": "BAR"},
    "buz": null,
    "qux": {},
    "quux": ""
  }
}

XML: empty element

<xml>
  <foo bar="BAR"/>
  <buz/>
  <qux></qux>
  <quux></quux>
</xml>

Empty Attribute

JavaScript: @ property name or null value

{
  "xml": {
    "@": "bar",
    "@baz": null,
    "foo": "FOO"
  }
}

XML: empty attribute

<xml bar baz>
    <foo>FOO</foo>
</xml>

Multiple Child Nodes

JavaScript: Array of String or Array of Object

{
  "xml": {
    "foo": ["BAR", "BAZ", "QUX"]
  }
}

XML: child nodes

<xml>
  <foo>BAR</foo>
  <foo>BAZ</foo>
  <foo>QUX</foo>
</xml>

Text Node with Attribute

JavaScript: # property name

{
  "xml": {
    "foo": {
      "@bar": "BAR",
      "#": "BAZ"
    }
  }
}

XML: text node

<xml>
  <foo bar="BAR">BAZ</foo>
</xml>

XML Declaration and Comment

JavaScript: ? and ! property name

{
  "?": "xml version=\"1.0\"",
  "!": "DOCTYPE note SYSTEM \"Note.dtd\"",
  "note": {
    "title": "FOO",
    "!": "-- comment --",
    "body": "BAR"
  }
}

XML:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
  <title>FOO</title>
  <!-- comment -->
  <body>BAR</body>
</note>

Fragment

JavaScript: # property name

{
  "plist": {
    "@version": "1.0",
    "dict": {
      "#": [
        {"key": "CFBundleDevelopmentRegion"},
        {"string": "ja"},
        {"key": "CFBundleIcons"},
        {"dict": null},
        {"key": "LSRequiresIPhoneOS"},
        {"true": null}
      ]
    }
  }
}

XML: child node list in order

<plist version="1.0">
  <dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>ja</string>
    <key>CFBundleIcons</key>
    <dict/>
    <key>LSRequiresIPhoneOS</key>
    <true/>
  </dict>
</plist>

CLI

$ echo '{"foo":{"@bar":"BAR","buz":"BUZ"}}' | ./node_modules/.bin/json2xml -2
<foo bar="BAR">
  <buz>BUZ</buz>
</foo>

LINKS

LICENSE

The MIT License (MIT)

Copyright (c) 2016-2023 Yusuke Kawasaki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

to-xml's People

Contributors

kawanet avatar

Stargazers

Wojciech Lechowicz avatar David Rud Pedersen avatar Blake Bauman avatar  avatar Dmytro Ukhin avatar gp avatar Divine avatar Seçkin Can Şahin avatar Endel Dreyer avatar

Watchers

 avatar James Cloos avatar

to-xml's Issues

CDATA

Nice module, but it doesn't seem to support CDATA correctly as it's always encoding it when it should leave it alone.

The input...

var p = {
name: "<![CDATA[This is <b>HTML</b>]]>"
};

Will output...

<name>&lt;![CDATA[This is &lt;b&gt;HTML&lt;/b&gt;]]&gt;</name>

Any help would be appreciated!

Best,
Mel

TS7016: Could not find a declaration file for module 'to-xml'

TS7016: Could not find a declaration file for module 'to-xml'. '.../node_modules/to-xml/dist/to-xml.mjs' implicitly has an 'any' type. There are types at '.../node_modules/to-xml/to-xml.d.ts', but this result could not be resolved when respecting package.json "exports". The 'to-xml' library may need to update its package.json or typings.

Option to omit "empty" tags

Hi. Nice work here. Congrats.

It would be nice to have an option to define a "empty" value type that would skip a tag to be rendered, and also childless parents because of this setting would not be rendered too.

For example:

let xml = toXML(objXml, null, 4, {skip_tags_with: null});  // Tags with null as value wont be rendered, nor its parent if childless

Thanks

HTML entities replace trailing and/or leading whitespace

The following JSON input

{
  "firstName": "dave "
}

Gives me this XML

<firstName>Dave&#x20;</firstName>

Why does this only happen to leading / trailing whitespace, and more importantly, is there a way for this package to just ignore this and let it be? Thanks in advance.

Compact element with attrs but with null content

At the moment, the following JSON object:

{
  'name': {
    '@attr': 'value',
    '#': null
  }
}

is converted to following XML fragment:

<name attr="value"></name>

Instead, it should be:

<name attr="value" />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.