expressjsHow can I parse XML data using Express.js?
Parsing XML data using Express.js is possible with the help of the xml2js package.
Below is an example code block that parses an XML string and prints the output to the console:
const xml2js = require('xml2js');
const xml = `<root>
<person>
<name>John</name>
<age>30</age>
</person>
</root>`;
const parseString = xml2js.parseString;
parseString(xml, (err, result) => {
if (err) {
console.error(err);
} else {
console.log(result);
}
});
Output example
{
root: {
person: [
{
name: [ 'John' ],
age: [ '30' ]
}
]
}
}
The code above consists of the following parts:
const xml2js = require('xml2js');
- This imports the xml2js package.const xml =
... ;
- This defines the XML string to be parsed.const parseString = xml2js.parseString;
- This retrieves the parseString function from the xml2js package.parseString(xml, (err, result) => { ... });
- This calls the parseString function to parse the XML string and print the result to the console.
Helpful links
More of Expressjs
- How do I find Express.js tutorials on YouTube?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I disable the X-Powered-By header in Express.js?
- How do Express.js and Spring Boot compare in terms of features and performance?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How do Express.js and Fastify compare in terms of performance and scalability?
- How can I set up unit testing for an Express.js application?
- How do I use Express.js to create a YouTube clone?
- How can I use Express.js and SQLite together to develop a web application?
- How can I use Express.js and PostgreSQL together to create a web application?
See more codes...