Louis
Louis

Reputation: 622

get a part of a string in an array

I am currently using nodejs and I have a function which return a body (String) its like this :

 <resourceDescriptors>
<resourceDescriptor name="AllAccounts" wsType="reportUnit" uriString="/reports/samples/AllAccounts" isNew="false">
    <label><![CDATA[Accounts Report]]></label>
    <description><![CDATA[All Accounts Report]]></description>
    <creationDate>1328803684197</creationDate>
    <resourceProperty name="PROP_RESOURCE_TYPE">
        <value><![CDATA[com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_PARENT_FOLDER">
        <value><![CDATA[/reports/samples]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_VERSION">
        <value><![CDATA[0]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_RU_ALWAYS_PROPMT_CONTROLS">
        <value><![CDATA[false]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_RU_CONTROLS_LAYOUT">
        <value><![CDATA[1]]></value>
    </resourceProperty>
</resourceDescriptor>
<resourceDescriptor name="Cascading_multi_select_report" wsType="reportUnit" uriString="/reports/samples/Cascading_multi_select_report" isNew="false">
    <label><![CDATA[Cascading multi select example report]]></label>
    <description><![CDATA[Example report with Cascading multi select input controls]]></description>
    <creationDate>1328803684289</creationDate>
    <resourceProperty name="PROP_RESOURCE_TYPE">
        <value><![CDATA[com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_PARENT_FOLDER">
        <value><![CDATA[/reports/samples]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_VERSION">
        <value><![CDATA[0]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_RU_ALWAYS_PROPMT_CONTROLS">
        <value><![CDATA[true]]></value>
    </resourceProperty>
    <resourceProperty name="PROP_RU_CONTROLS_LAYOUT">
        <value><![CDATA[1]]></value>
    </resourceProperty>
</resourceDescriptor>
                ....
</resourceDescriptors>

So I want to get all the FIRST name part (without the quotes) from this string and put it in an array or a list (in javascript), like here I would like to have :

list[0]=AllAccounts
list[1]=Cascading_multi_select_report

I tried several way but it's not working, could you help me ? Thanks !

Upvotes: 0

Views: 226

Answers (3)

maerics
maerics

Reputation: 156612

Try using node-expat, a (fast!) streaming XML parsing library. Assuming your XML document is (or can be) stored as a string:

var xp = require('node-expat');

function getResourceDescriptorNames(xmlStr) {
  var names = [], parser = new xp.Parser('UTF-8');
  parser.on('startElement', function(name, attr) {
    if (name === 'resourceDescriptor') names.push(attr.name);
  }).parse(xmlStr);
  return names;
}

getResourceDescriptorNames(myXmlString);
// => ['AllAccounts', 'Cascading_multi_select_report']

Note that if your XML document comes from a stream instead of a buffer and you don't wish to buffer it you can simply call parser.parse(data) for each chunk of data and it will work fine.

Upvotes: 1

Linus Thiel
Linus Thiel

Reputation: 39261

Using xml2js:

var xml2js = require('xml2js'),
    parser = new xml2js.Parser();

parser.parseString(body, function(err, result) {
  var names = result.resourceDescriptor.map(function(resourceDescriptor) {
    return resourceDescriptor['@'].name;
  });
  console.log(names);
  // => [ 'AllAccounts', 'Cascading_multi_select_report' ]
});

Note: I'm not that familiar with xml2js, there might be better libraries out there, especially if you need to parse large documents.

Upvotes: 2

ThePrimeagen
ThePrimeagen

Reputation: 4582

Here, this will make your life easier.

Learn to use the DOM parser provided in javascript http://www.w3schools.com/dom/default.asp

Upvotes: 1

Related Questions