Node.js is a server-side running environment built on the Chrome V8 JavaScript engine. It can write server-side code using JavaScript and is currently one of the most popular server-side programming languages. Among them, JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in network data transmission and data storage. XML (Extensible Markup Language) is a language used to store and transmit data, and is also widely used in data transmission and storage.
In Node.js, it is a common operation to convert JSON format to XML format, which can better adapt to certain data transfer protocols or data storage protocols. This article will introduce how to convert JSON format to XML format using modules in Node.js.
XMLBuilder is a module in Node.js used to build XML documents. It supports chain calls and simple syntax, allowing developers to quickly create XML documents. A complete code example is given below:
const builder = require('xmlbuilder'); const jsonObj = { "book": { "title": "Harry Potter and the Philosopher's Stone", "author": "J.K. Rowling", "year": "1997" } }; const xmlObj = builder.create(jsonObj, { encoding: 'utf-8' }); const xmlStr = xmlObj.end({ pretty: true }); console.log(xmlStr);
This example code converts a JSON object into an XML string. We first introduced the xmlbuilder module, and then defined a JSON object jsonObj
, which contains the book title, author and year of publication. Next, we use the builder.create()
method to convert the JSON object into an XML object xmlObj
, and specify the encoding format as UTF-8. Finally, we use the xmlObj.end()
method to format the XML into a string and output it to the console.
In the above code, in the process of using XMLBuilder to build an XML document, a chain call method is used to add child nodes or attributes through continuous dot operators (.). XMLBuilder can also use JSON form to build nodes, such as {name: "value"}
. Chain calls and the construction tree structure in JSON form make the XMLBuilder API very simple and convenient to use.
xml2js is a module that parses XML documents into JavaScript objects, and also provides the function of converting JavaScript objects into XML documents. The following is a sample code for converting a JSON object into an XML document:
const xml2js = require("xml2js"); const jsonObj = { "book": { "title": "Harry Potter and the Philosopher's Stone", "author": "J.K. Rowling", "year": "1997" } }; const xmlBuilder = new xml2js.Builder({ rootName: "books", renderOpts: { pretty: true } }); const xmlStr = xmlBuilder.buildObject(jsonObj); console.log(xmlStr);
In this sample code, we first introduce the xml2js module and then define a JSON object jsonObj
. Next, we used the new xml2js.Builder()
method to create an XMLBuilder object xmlBuilder
, and specified the root node name as "books" and the output format as beautification style. Finally, we use the xmlBuilder.buildObject()
method to convert the JSON object into XML format and output it to the console.
It should be noted that when using xml2js to convert JSON to XML format, you need to use the new xml2js.Builder()
method to create an XMLBuilder object in order to perform the conversion operation. At the same time, it is also very convenient to use xml2js to parse XML documents. Through the parseString()
method provided by xml2js, we can parse the XML document into a JavaScript object, as shown in the following code:
const xml2js = require("xml2js"); const xmlStr = '<books><book><title>Harry Potter and the Philosopher's Stone</title><author>J.K. Rowling</author><year>1997</year></book></books>'; xml2js.parseString(xmlStr, { explicitArray: false }, (err, result) => { if (err) { console.error(err); return; } console.log(result); });
In this code example, we use the xml2js.parseString()
method to parse an XML string into a JavaScript object. In the parseString()
method, we also specify the explicitArray:false
parameter to avoid generating redundant arrays when parsing XML documents. After parsing the XML document into a JavaScript object, we only need to process the JavaScript object in the callback function.
Node.js provides some modules to implement the function of converting JSON format to XML format, among which XMLBuilder and xml2js are both good choices. Using XMLBuilder, we can easily build XML documents through chain operations and JSON form. xml2js provides the function of parsing XML documents into JavaScript objects to facilitate subsequent processing. No matter what kind of module it is, making full use of the modules provided by Node.js can simplify the code and make development more efficient and convenient.
The above is the detailed content of nodejs json to xml. For more information, please follow other related articles on the PHP Chinese website!