Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
The jstoxml package is a simple and lightweight JavaScript library for converting JavaScript objects into XML strings. It is useful for applications that need to generate XML data from JSON or JavaScript objects.
Convert JavaScript Object to XML
This feature allows you to convert a JavaScript object into an XML string. The code sample demonstrates how to use the `toXML` method to transform a simple object into XML format.
{"const jstoxml = require('jstoxml'); const data = { name: 'John', age: 30, city: 'New York' }; const xml = jstoxml.toXML(data); console.log(xml);": ""}
Custom XML Tag Names
This feature allows you to specify custom XML tag names using the `_name` property. The code sample shows how to create a custom root tag for the XML output.
{"const jstoxml = require('jstoxml'); const data = { _name: 'person', _content: { name: 'John', age: 30 } }; const xml = jstoxml.toXML(data); console.log(xml);": ""}
Attributes in XML Tags
This feature allows you to add attributes to XML tags using the `_attrs` property. The code sample demonstrates how to include attributes in the XML output.
{"const jstoxml = require('jstoxml'); const data = { person: { _attrs: { id: 1 }, name: 'John', age: 30 } }; const xml = jstoxml.toXML(data); console.log(xml);": ""}
xml2js is a popular library for converting XML to JavaScript objects and vice versa. It offers more comprehensive XML parsing capabilities compared to jstoxml, which is primarily focused on converting JavaScript objects to XML.
xmlbuilder is a versatile library for building XML documents. It provides a more flexible and feature-rich API for constructing XML, including support for namespaces and complex document structures, whereas jstoxml is simpler and more straightforward for basic conversions.
fast-xml-parser is known for its high performance in parsing XML to JSON and vice versa. It offers both parsing and building functionalities, making it a more comprehensive solution compared to jstoxml, which focuses on object-to-XML conversion.
Everyone loves JSON, and more and more folks want to move that direction, but we still need things outputted in XML! Particularly for RSS feeds and Podcasts.
This is inspired by node-jsontoxml, which was found to be a bit too rough around the edges. jstoxml attempts to fix that by being more flexible.
&
-> &
, etc) (optional)For instance:
jstoxml.toXML({
a: '1',
foo: '',
b: '2'
});
// Output: <a>1</a><foo></foo><b>2</b>
jstoxml.toXML({
a: '1',
foo: null,
b: '2'
});
// Output: <a>1</a>foo<b>2</b>
First you'll want to require jstoxml in your script, and assign the result to the namespace variable you want to use (in this case jstoxml):
var jstoxml = require('jstoxml');
jstoxml has a very simple interface: jstoxml.toXML(input, addHeader [Boolean], indent [String]);
jstoxml.toXML({
foo: 'bar',
foo2: 'bar2'
});
Output:
<foo>bar</foo><foo2>bar2</foo2>
jstoxml.toXML([
{foo: 'bar'},
{foo2: 'bar2'}
]);
Output:
<foo>bar</foo><foo2>bar2</foo2>
Because we can't have duplicate keys in objects, we have to take advantage of arrays to get duplicate tag names:
jstoxml.toXML([
{foo: 'bar'},
{foo: 'bar2'}
]);
Output:
<foo>bar</foo><foo>bar2</foo>
jstoxml.toXML({
_name: 'foo',
_content: 'bar',
_attrs: {
a: 'b',
c: 'd'
}
});
Output:
<foo a="b" c="d">bar</foo>
To output text content, set a key to null:
jstoxml.toXML({
'text1': null,
foo: 'bar',
'text2': null
});
Output:
text1<foo>bar</foo>text2
jstoxml.toXML({
a: {
foo: 'bar',
foo2: 'bar2'
}
}, {header: false, indent: ' '});
Output:
<a>
<foo>bar</foo>
<foo2>bar2</foo2>
</a>
jstoxml.toXML({
ooo: {
_name: 'foo',
_attrs: {
a: 'b'
},
_content: {
_name: 'bar',
_attrs: {
c: 'd'
}
}
}
}, {header: false, indent: ' '});
Output:
<ooo>
<foo a="b">
<bar c="d"/>
</foo>
</ooo>
Note that cases like this might be especially hard to read because of the deep nesting, so it's recommend you use something like this pattern instead, which breaks it up into more readable pieces:
var bar = {
_name: 'bar',
_attrs: {
c: 'd'
}
};
var foo = {
_name: 'foo',
_attrs: {
a: 'b'
},
_content: bar
}
return jstoxml.toXML({
ooo: foo
}, {header: false, indent: ' '})
jstoxml.toXML({
onePlusTwo: function(){
return 1 + 2;
},
date: function(){
return new Date();
}
});
Output:
<onePlusTwo>3</onePlusTwo><date>Sat Jul 30 2011 17:49:52 GMT+0900 (JST)</date>
jstoxml.toXML({
_name: 'rss',
_attrs: {
version: '2.0'
},
_content: {
channel: [
{title: 'RSS Example'},
{description: 'Description'},
{link: 'google.com'},
{lastBuildDate: function(){
return new Date();
}},
{pubDate: function(){
return new Date();
}},
{language: 'en'},
{item: {
title: 'Item title',
link: 'Item link',
description: 'Item Description',
pubDate: function(){
return new Date();
}
}},
{item: {
title: 'Item2 title',
link: 'Item2 link',
description: 'Item2 Description',
pubDate: function(){
return new Date();
}
}}
]
}
}, {header: true, indent: ' '});
Output:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>RSS Example</title>
<description>Description</description>
<link>google.com</link>
<lastBuildDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</lastBuildDate>
<pubDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</pubDate>
<language>en</language>
<item>
<title>Item title</title>
<link>Item link</link>
<description>Item Description</description>
<pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
</item>
<item>
<title>Item2 title</title>
<link>Item2 link</link>
<description>Item2 Description</description>
<pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>
</item>
</channel>
</rss>
(see the Apple docs for more information)
jstoxml.toXML({
_name: 'rss',
_attrs: {
'xmlns:itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
version: '2.0'
},
_content: {
channel: [
{title: 'Title'},
{link: 'google.com'},
{language: 'en-us'},
{copyright: 'Copyright 2011'},
{'itunes:subtitle': 'Subtitle'},
{'itunes:author': 'Author'},
{'itunes:summary': 'Summary'},
{description: 'Description'},
{'itunes:owner': {
'itunes:name': 'Name',
'itunes:email': 'Email'
}},
{
_name: 'itunes:image',
_attrs: {
href: 'image.jpg'
}
},
{
_name: 'itunes:category',
_attrs: {
text: 'Technology'
},
_content: {
_name: 'itunes:category',
_attrs: {
text: 'Gadgets'
}
}
},
{
_name: 'itunes:category',
_attrs: {
text: 'TV & Film'
}
},
{
item: [
{title: 'Podcast Title'},
{'itunes:author': 'Author'},
{'itunes:subtitle': 'Subtitle'},
{'itunes:summary': 'Summary'},
{'itunes:image': 'image.jpg'},
{
_name: 'enclosure',
_attrs: {
url: 'http://example.com/podcast.m4a',
length: '8727310',
type: 'audio/x-m4a'
}
},
{guid: 'http://example.com/archive/aae20050615.m4a'},
{pubDate: 'Wed, 15 Jun 2011 19:00:00 GMT'},
{'itunes:duration': '7:04'},
{'itunes:keywords': 'salt, pepper, shaker, exciting'}
]
},
{
item: [
{title: 'Podcast2 Title'},
{'itunes:author': 'Author2'},
{'itunes:subtitle': 'Subtitle2'},
{'itunes:summary': 'Summary2'},
{'itunes:image': 'image2.jpg'},
{
_name: 'enclosure',
_attrs: {
url: 'http://example.com/podcast2.m4a',
length: '655555',
type: 'audio/x-m4a'
}
},
{guid: 'http://example.com/archive/aae2.m4a'},
{pubDate: 'Wed, 15 Jul 2011 19:00:00 GMT'},
{'itunes:duration': '11:20'},
{'itunes:keywords': 'foo, bar'}
]
}
]
}
}, {header: true, indent: ' '});
Output:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Title</title>
<link>google.com</link>
<language>en-us</language>
<copyright>Copyright 2011</copyright>
<itunes:subtitle>Subtitle</itunes:subtitle>
<itunes:author>Author</itunes:author>
<itunes:summary>Summary</itunes:summary>
<description>Description</description>
<itunes:owner>
<itunes:name>Name</itunes:name>
<itunes:email>Email</itunes:email>
</itunes:owner>
<itunes:image href="image.jpg"/>
<itunes:category text="Technology">
<itunes:category text="Gadgets"/>
</itunes:category>
<itunes:category text="TV & Film"/>
<item>
<title>Podcast Title</title>
<itunes:author>Author</itunes:author>
<itunes:subtitle>Subtitle</itunes:subtitle>
<itunes:summary>Summary</itunes:summary>
<itunes:image>image.jpg</itunes:image>
<enclosure url="http://example.com/podcast.m4a" length="8727310" type="audio/x-m4a"/>
<guid>http://example.com/archive/aae20050615.m4a</guid>
<pubDate>Wed, 15 Jun 2011 19:00:00 GMT</pubDate>
<itunes:duration>7:04</itunes:duration>
<itunes:keywords>salt, pepper, shaker, exciting</itunes:keywords>
</item>
<item>
<title>Podcast2 Title</title>
<itunes:author>Author2</itunes:author>
<itunes:subtitle>Subtitle2</itunes:subtitle>
<itunes:summary>Summary2</itunes:summary>
<itunes:image>image2.jpg</itunes:image>
<enclosure url="http://example.com/podcast2.m4a" length="655555" type="audio/x-m4a"/>
<guid>http://example.com/archive/aae2.m4a</guid>
<pubDate>Wed, 15 Jul 2011 19:00:00 GMT</pubDate>
<itunes:duration>11:20</itunes:duration>
<itunes:keywords>foo, bar</itunes:keywords>
</item>
</channel>
</rss>
jstoxml.toXML({
foo: '<a>',
bar: '"b"',
baz: '\'&whee\''
},
{
filter: {
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'&': '&'
}
});
Output:
<foo><a></foo><bar>"b"</bar><baz>'&whee'</baz>
jstoxml.toXML({
foo: '',
bar: 'whee'
});
Output:
<foo></foo><bar>whee</bar>
FAQs
Converts JavaScript/JSON to XML (for RSS, Podcasts, AMP, etc.)
The npm package jstoxml receives a total of 66,654 weekly downloads. As such, jstoxml popularity was classified as popular.
We found that jstoxml demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.