在字符串中搜索模式并存储其值
searching a pattern in string and storing its values
我有这个字符串格式的xml。我想要获得名为"ColumnName"的属性。请帮我在数组中查找并返回它。波纹管是格式
<EntitySet Name="Department" VersionConflict="False" xmlns:">
<StringAttribute Caption="Department Number" ColumnName="fdeptno" Description="dept no" IsPrimaryKeyMember="True" IsRequired="True" MaxLength="2" Name="fdeptno" RequiredAdherenceMessage="EMPTY_ACCT(Department Number)" />
<StringAttribute Caption="Description" ColumnName="fdeptdesc" Description="department" IsRequired="True" MaxLength="35" Name="fdeptdesc" RequiredAdherenceMessage="DESCR_EMPTY" />
<StringAttribute Caption="Holiday Pay Acct" ColumnName="fholaccno" ContentType="GeneralLedgerAccount" Description="holiday" IsRequired="True" MaxLength="25" Name="fholaccno" RequiredAdherenceMessage="HOLIDAY_PAY_ACC">
<StringAttribute.Format>
<MaskFormat Mask="AAAAA-AA" />
</StringAttribute.Format>
<PropertyReference EntityName="glmast" Filter="GLMAST.flinactive=0" Name="prdept_fholaccno" ObjectName="ChartofAccountsMaintenance" PropertyName="fcacctnum" UseIndexView="True" />
</StringAttribute>
<StringAttribute Caption="Other Pay Acct" ColumnName="fothaccno" ContentType="GeneralLedgerAccount" Description="other" IsRequired="True" MaxLength="25" Name="fothaccno" RequiredAdherenceMessage="OTHER_PAY_EMPTY">
<StringAttribute.Format>
<MaskFormat Mask="AAAAA-AA" />
</StringAttribute.Format>
<PropertyReference EntityName="glmast" Filter="GLMAST.flinactive=0" Name="prdept_fothaccno" ObjectName="ChartofAccountsMaintenance" PropertyName="fcacctnum" UseIndexView="True" />
</StringAttribute>
<StringAttribute Caption="Sick Pay Acct" ColumnName="fsickaccno" ContentType="GeneralLedgerAccount" Description="sick" IsRequired="True" MaxLength="25" Name="fsickaccno" RequiredAdherenceMessage="SICK_PAY_EMPTY">
<StringAttribute.Format>
<MaskFormat Mask="AAAAA-AA" />
</StringAttribute.Format>
<PropertyReference EntityName="glmast" Filter="GLMAST.flinactive=0" Name="prdept_fsickaccno" ObjectName="ChartofAccountsMaintenance" PropertyName="fcacctnum" UseIndexView="True" />
</StringAttribute>
<StringAttribute Caption="Vacation Pay Acct" ColumnName="fvacaccno" ContentType="GeneralLedgerAccount" Description="vacation" IsRequired="True" MaxLength="25" Name="fvacaccno" RequiredAdherenceMessage="VAC_PAY_ACCT_EMPTY">
<StringAttribute.Format>
<MaskFormat Mask="AAAAA-AA" />
</StringAttribute.Format>
<PropertyReference EntityName="glmast" Filter="GLMAST.flinactive=0" Name="prdept_fvacaccno" ObjectName="ChartofAccountsMaintenance" PropertyName="fcacctnum" UseIndexView="True" />
</StringAttribute>
<IntegerAttribute ColumnName="identity_column" Name="identity_column" />
<ByteArrayAttribute ColumnName="timestamp_column" Name="timestamp_column" Searchable="False" />
<StringAttribute Caption="facility" ColumnName="fac" Description="facility" MaxLength="20" Name="fac" />
一种方法是使用DOMParser
解析字符串。然后,您可以循环浏览所有元素并检查属性。不过,我更喜欢使用querySelectorAll
,因为生成的代码简洁有效:
var xmlDoc = (new DOMParser).parseFromString(xml_string, 'text/xml');
var elementsWithAttr = xmlDoc.querySelectorAll('[ColumnName]');
var values = [].map.call(elementsWithAttr, function(element) {
return element.attributes.getNamedItem('ColumnName').nodeValue;
});
演示:http://jsfiddle.net/hm3Cn/(我已经接受了您的XML输入,通过删除第一行的xmlns:"
并在末尾添加</EntitySet>
使其有效)。
相关文章:
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 在字符串中搜索模式并存储其值
- 如何在变量中存储多个模式参数并在 JavaScript 中打印该变量的值
- 如何获取所有旅行模式的距离和持续时间,例如驾驶,公交,骑自行车,在我的页面中步行以及将其存储在数据库中
- 如何在引导模式上设置本地存储
- Knockout.js :使用存储库模式绑定到点击事件上的函数
- HTML5 - 离线模式,本地存储和安全都在船上
- IE8 中的 IE7 模式 - 本地存储不是未定义的
- 将 json 数据存储在代理模式内的变量中
- 用于存储多个配置对象的模式
- 用于存储/检索Backbone.js的模式查看与dom元素关联的实例
- Web存储(sessionStorage和localStorage)处于私人浏览模式(隐姓埋名)
- Ajax和本地/会话存储模式
- 如何从一个JSP页面调用另一个JSP页面中的引导模式存储
- 如何在离线模式下存储修改后的网页内容,然后再回到在线状态
- 使用高效的主键模式在SQL数据库中存储2D映射
- JS -工厂模式覆盖私有变量存储在内存中
- 我如何存储数据使用这种模式-对象内对象内对象或对象内对象内数组
- 用于查找关键字之间的模式并将其存储在组中的正则表达式
- IE 11企业模式下未触发本地存储事件