rtv
rtv

Reputation: 165

Extract values from nested XML using Node JS

I'm writing a REST service in node which takes XML as input, however when I parse it, only the 1st level nodes are displayed, the nested ones are displayed as objects.

I cannot hard-code the XML elements to be read because as per the specification there are optional elements, so it has to be dynamic.

I'm using body-parser-xml to parse the XML.

var express = require('express'),
    bodyParser = require('body-parser');

require('body-parser-xml')(bodyParser);

//var xmlparser = require('express-xml-bodyparser');

var app = express();

app.use(bodyParser.xml({
  limit: '1MB',   // Reject payload bigger than 1 MB 
  xmlParseOptions: {
    normalize: true,     // Trim whitespace inside text nodes 
    normalizeTags: true, // Transform tags to lowercase 
    explicitArray: false, // Only put nodes in array if >1 
    preserveChildrenOrder: true
  }
}));

// app.use(xmlparser());

app.post('/users', function(req, res, body) {
  // Any request with an XML payload will be parsed 
  // and a JavaScript object produced on req.body 
  // corresponding to the request payload. 
  console.log(req.body);
  var parsedXml = req.body;
  console.log(parsedXml.classes);
  res.status(200).end();
});

var http = require('http');
http.createServer(app).listen(3000);

My XML input for REST is

<?xml version="1.0" encoding="UTF-8"?>
<Schools>
<School>
    <Name>Some Name</Name>
    <City>Some City</City>
    <Classes>
        <Class>
            <Name>Class 1</Name>
            <OnRoll>20</OnRoll>
            <Students>
                <Student>
                    <Name>Student 1</Name>
                    <Age>10</Age>
                </Student>
                <Student>
                    <Name>Student 2</Name>
                    <Age>11</Age>
                </Student>
            </Students>
        </Class>
        <Class>
            <Name>Class 2</Name>
            <OnRoll>30</OnRoll>
            <Students>
                <Student>
                    <Name>Student 21</Name>
                    <Age>12</Age>
                </Student>
                <Student>
                    <Name>Student 22</Name>
                    <Age>13</Age>
                </Student>
            </Students>
        </Class>
    </Classes>
    <Labs>
        <Lab>
            <Name>Science Lab 1</Name>
            <Subject>Physics</Subject>
        </Lab>
        <Lab>
            <Name>Science Lab 2</Name>
            <Subject>Chemistry</Subject>
        </Lab>
    </Labs>
    </School>
</Schools>

And the output is

D:\Tryouts\myapp>node xmlParser.js
{ schools:
   { school:
      { name: 'Some Name',
        city: 'Some City',
        classes: [Object],
        labs: [Object] } } }
undefined

Basically I'm looking at something which would automatically read the nested XML and display it.

I'm using Chrome REST plugin to invoke this service http://localhost:3000/users with Method as POST and Content-Type as application/xml.

Requesting your help to resolve this.

Upvotes: 1

Views: 1542

Answers (1)

David Espino
David Espino

Reputation: 2187

You may want to use the good and nice Util package. Try this:

const util = require('util');
// Parse your xml file   
console.log(util.inspect(parsedXml.classes, { depth: null, showHidden: false }));

Hope this helps.

Upvotes: 2

Related Questions