Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
js2xmlparser
Advanced tools
The js2xmlparser npm package is a library that allows users to convert JavaScript objects into XML. It is particularly useful for creating XML data from JSON format, supporting various customization options to handle complex data structures and attributes.
Basic XML Conversion
Converts a simple JavaScript object into XML. The example shows converting a person object with first and last names into an XML string.
const js2xmlparser = require('js2xmlparser');
let person = {
firstName: 'John',
lastName: 'Doe'
};
let xml = js2xmlparser.parse('person', person);
console.log(xml);
XML with Attributes
Demonstrates how to include attributes in the XML output. The '@' property is used to specify attributes for the XML element.
const js2xmlparser = require('js2xmlparser');
let person = {
'@': {
id: '12345'
},
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@example.com'
};
let xml = js2xmlparser.parse('person', person);
console.log(xml);
Handling Arrays
Shows how to handle JavaScript arrays to produce nested XML elements. Each object in the 'members' array becomes a separate XML element.
const js2xmlparser = require('js2xmlparser');
let family = {
lastName: 'Doe',
members: [
{
firstName: 'John',
relationship: 'Father'
},
{
firstName: 'Jane',
relationship: 'Mother'
}
]
};
let xml = js2xmlparser.parse('family', family);
console.log(xml);
xml2js is another popular npm package that primarily focuses on converting XML data to JavaScript objects but also supports XML building. It differs from js2xmlparser by providing bi-directional conversion (XML to JS and JS to XML) and has a slightly different API for handling options.
xmlbuilder is an npm package that provides an easy interface to build XML documents from JavaScript. Unlike js2xmlparser, which is more focused on converting existing JS objects to XML, xmlbuilder allows for more dynamic creation of XML documents through a fluent interface.
js2xmlparser is a Node.js module that parses JavaScript objects into XML.
Since XML is a data-interchange format, js2xmlparser is designed primarily for JSON-type objects, arrays and primitive data types, like many of the other JavaScript to XML parsers currently available for Node.js.
However, js2xmlparser is capable of parsing any object, including native
JavaScript objects such as Date
and RegExp
, by taking advantage of each
object's toString
function or, if this function does not exist, the String
constructor.
js2xmlparser also has support for the Map
and Set
objects introduced in
ECMAScript 2015, treating them as JSON-type objects and arrays respectively.
Support for Map
s is necessary to generate XML with elements in a specific
order, since JSON-type objects do not guarantee insertion order. Map
keys are
always converted to strings using the method described above.
js2xmlparser also supports a number of constructs unique to XML:
js2xmlparser can also pretty-print the XML it outputs.
The easiest way to install js2xmlparser is using npm:
npm install js2xmlparser
You can also build js2xmlparser from source using npm:
git clone https://github.com/michaelkourlas/node-js2xmlparser.git
npm install
npm run-script build
The build
script will build the production variant of js2xmlparser, run all
tests, and build the documentation.
You can build the production variant without running tests using the script
prod
. You can also build the development version using the script dev
.
The only difference between the two is that the development version includes
source maps.
The documentation for the current version is available here.
You can also build the documentation using npm:
npm run-script docs
The following example illustrates the basic usage of js2xmlparser:
var js2xmlparser = require("js2xmlparser");
var obj = {
"@": {
type: "natural",
},
firstName: "John",
lastName: "Smith",
dateOfBirth: new Date(1964, 7, 26),
address: {
"@": {
type: "home",
},
streetAddress: "3212 22nd St",
city: "Chicago",
state: "Illinois",
zip: 10000,
},
phone: [
{
"@": {
type: "home",
},
"#": "123-555-4567",
},
{
"@": {
type: "cell",
},
"#": "890-555-1234",
},
{
"@": {
type: "work",
},
"#": "567-555-8901",
},
],
email: "john@smith.com",
};
console.log(js2xmlparser.parse("person", obj));
This example produces the following XML:
<?xml version='1.0'?>
<person type='natural'>
<firstName>John</firstName>
<lastName>Smith</lastName>
<dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Summer Time)</dateOfBirth>
<address type='home'>
<streetAddress>3212 22nd St</streetAddress>
<city>Chicago</city>
<state>Illinois</state>
<zip>10000</zip>
</address>
<phone type='home'>123-555-4567</phone>
<phone type='cell'>890-555-1234</phone>
<phone type='work'>567-555-8901</phone>
<email>john@smith.com</email>
</person>
Additional examples can be found in the examples directory.
js2xmlparser includes a set of tests to verify core functionality. You can run the tests using npm:
npm run-script test-prod
The only difference between the test-prod
and test-dev
scripts is that the
development version includes source maps.
js2xmlparser is licensed under the Apache License 2.0.
5.0.0
FAQs
Parses JavaScript objects into XML
The npm package js2xmlparser receives a total of 2,728,634 weekly downloads. As such, js2xmlparser popularity was classified as popular.
We found that js2xmlparser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.