0

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 !

1
  • If you use jQuery maybe this will be useful: jQuery.parseXML() Commented Mar 13, 2012 at 14:39

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Here, this will make your life easier.

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

11 Comments

i only did .+ because i do not know what type of "names" are valid. there could be -, _, digits, and maybe more! :)
Thanks ! Well I am not using Jquery, can you do me the "forloop" version ? I am really new to javascript I tried with a for each not working(It's can be letter and digits)
THanks for the new version , but it say "k is not declared"
Oops, i copied and pasted, my bad
Thanks again ,unfortunately it's not working , it's giving me the almost the same full line (twice) like this : 0 => <resourceDescriptor name="AllAccounts" wsType="reportUnit" uriString="/reports/AllAccounts" isNew="false" and 1 => AllAccounts" wsType="reportUnit" uriString="/reports/AllAccounts" isNew="false
|
1

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.

1 Comment

Yeah thanks i want to try that, i am just searching how to install this module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.