Javascript 在字符串中搜索模式并存储其值

Javascript 在字符串中搜索模式并存储其值,javascript,xml,xml-parsing,Javascript,Xml,Xml Parsing,我有这个字符串格式的xml。我想得到名为“ColumnName”的属性。请帮助我查找并以数组形式返回它。下面是格式 <EntitySet Name="Department" VersionConflict="False" xmlns:"> <StringAttribute Caption="Department Number" ColumnName="fdeptno" Description="dept no" IsPrimaryKeyMember="True" IsReq

我有这个字符串格式的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" />

实现这一点的一种方法是对字符串进行解析。然后,可以遍历所有元素并检查属性。但我更喜欢使用,因为生成的代码简洁有效:

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;
});

演示:(我接受了您的XML输入,通过删除
xmlns使其有效:“
在第一行,并在末尾追加
).

在众多的
ColumnName
中,哪一个?你能发布一些你尝试过的代码吗?顺便说一句,那不是有效的XML。
没有关闭,并且第一个标记中有一个不匹配的引号。它是从一个这样的函数中获取的。我同意它没有正确关闭。但是我想知道如何从变量a解析它nd获取上述字符串中ColumnName的所有值。@ShishirKumarM正确的解决方案取决于输入是否为有效的XML。是吗?因此此行返回元素.attributes.getNamedItem('ColumnName').nodeValue;应进行更改以使值存储在数组中format@ShishirKumarM不,
values
已经是一个数组。如果查看,您将看到控制台中正在记录一个数组。
[].map.call(,)
基于为类似数组的对象中的每个元素执行
的返回值创建一个数组。