我目前正在使用nodejs,我有一个函数,它返回一个body(String),如下所示:
<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>
所以我想从这个字符串中获取所有 FIRST 名称部分(不带引号)并将其放在数组或列表中(在javascript中),就像我想要的那样:
list[0]=AllAccounts
list[1]=Cascading_multi_select_report
我尝试了几种方法,但它不起作用,你能帮助我吗? 谢谢!
答案 0 :(得分:2)
使用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' ]
});
注意:我不熟悉xml2js
,可能有更好的库,特别是如果你需要解析大型文档。
答案 1 :(得分:1)
在这里,这将使您的生活更轻松。
学习使用javascript中提供的DOM解析器 http://www.w3schools.com/dom/default.asp
答案 2 :(得分:1)
尝试使用node-expat,一个(快速!)流式XML解析库。假设您的XML文档是(或可以)存储为字符串:
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']
请注意,如果您的XML文档来自流而不是缓冲区,并且您不希望缓冲它,则只需为每个数据块调用parser.parse(data)
,它就可以正常工作。