how to list all tags xml in node.js

297 views Asked by At

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- 1 = video , 2 = html , 3 = pdf -->
<stage_type>2</stage_type>
<resource_name>http://www.XXX</resource_name>

I want to get <resource_name>. I'm using xml2js. When I use:

var parser = new xml2js.Parser();
                        fs.readFile('./' + file, function(err, data) {
                            parser.parseString(data, function (err, result) {
                                console.dir(result);
                                console.log('Done');
                            });
                        });

I got <stage_type> and no all xml file. (<stage_type> and <resource_name>).
I know that I don't have a root tag, but I need to leave it like that.

1

There are 1 answers

0
Dan Field On BEST ANSWER

Your XML file isn't valid XML. It has multiple root nodes. Try parsing XML like this and it should work:

<?xml version="1.0" encoding="utf-8"?>
<!-- 1 = video , 2 = html , 3 = pdf -->
<resource>
  <stage_type>2</stage_type>
  <resource_name>http://www.XXX</resource_name>
</resource>

You could rename that resource node to whatever you want, but that's why xml2js is missing the resource_name. I don't know of any option to have xml2js process a fragment (which might make it accept this type of XML).