Xml in JavaScript
Here’s the translation of the XML example from Go to JavaScript, formatted in Markdown suitable for Hugo:
Our example demonstrates how to work with XML in JavaScript. We’ll use the xml2js
library for parsing and building XML.
First, let’s install the necessary dependencies:
npm install xml2js
Now, let’s look at the code:
const xml2js = require('xml2js');
// Plant will be mapped to XML. We use a class to define the structure.
class Plant {
constructor(id, name, origin) {
this.id = id;
this.name = name;
this.origin = origin;
}
toString() {
return `Plant id=${this.id}, name=${this.name}, origin=${this.origin}`;
}
}
async function main() {
const coffee = new Plant(27, "Coffee", ["Ethiopia", "Brazil"]);
// Create a new XML builder
const builder = new xml2js.Builder({rootName: 'plant'});
// Convert the coffee object to XML
const xml = builder.buildObject({
$: {id: coffee.id},
name: coffee.name,
origin: coffee.origin
});
console.log(xml);
// To add a generic XML header to the output, prepend it explicitly.
console.log('<?xml version="1.0" encoding="UTF-8"?>\n' + xml);
// Use parseString to parse a string of XML into a JavaScript object
const parser = new xml2js.Parser({explicitArray: false});
const result = await parser.parseStringPromise(xml);
console.log(new Plant(
result.plant.$.id,
result.plant.name,
result.plant.origin
).toString());
const tomato = new Plant(81, "Tomato", ["Mexico", "California"]);
// Demonstrate nesting of XML elements
const nesting = {
nesting: {
parent: {
child: {
plant: [
{
$: {id: coffee.id},
name: coffee.name,
origin: coffee.origin
},
{
$: {id: tomato.id},
name: tomato.name,
origin: tomato.origin
}
]
}
}
}
};
const nestedXml = builder.buildObject(nesting);
console.log(nestedXml);
}
main().catch(console.error);
This script demonstrates several key aspects of XML processing in JavaScript:
We define a
Plant
class to represent our data structure.We use the
xml2js.Builder
to convert JavaScript objects to XML. The$
property is used to specify attributes.We demonstrate how to add an XML header to the output.
We use
xml2js.Parser
to parse XML strings back into JavaScript objects. TheexplicitArray: false
option prevents single elements from being wrapped in arrays.We show how to create more complex nested XML structures.
When you run this script, you’ll see output similar to this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
Plant id=27, name=Coffee, origin=Ethiopia,Brazil
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nesting>
<parent>
<child>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<plant id="81">
<name>Tomato</name>
<origin>Mexico</origin>
<origin>California</origin>
</plant>
</child>
</parent>
</nesting>
This example showcases how to create, parse, and manipulate XML in JavaScript, providing similar functionality to the original example.