我正在使用 .find 方法来获取 xml 标签的值。在我的 xml 中,如果 xmltag 重复,它会给我所有标签的值。 示例
<Response>
<Result>0</Result>
<Message>Tenant Information fetched for tenantID=1</Message>
<Data>
<Tenants>
<row>
<TenantID>1</TenantID>
<TenantTypeID>1</TenantTypeID>
<CardCode>CLINSH0184</CardCode>
<CustomerName>S. H. Pitkar Orthotools Pvt Ltd</CustomerName>
<CustomerGroup>VAT Customer</CustomerGroup>
<ServiceContract>69</ServiceContract>
<SUserID>S0004493123</SUserID>
<ContactPerson>Nupur Pitkar</ContactPerson>
<Phone1>123</Phone1>
<Phone2>456</Phone2>
<Mobile>789</Mobile>
<Email>abcd@inecom.com.sg</Email>
<StartDate>2014-01-01T00:00:00</StartDate>
<EndDate>2015-01-01T00:00:00</EndDate>
<Active>1</Active>
<B1Version>8.82</B1Version>
<PatchLevel>12</PatchLevel>
<SqlVersion>2008</SqlVersion>
<ServiceURL>http://localhost:8932/CRMService.asmx</ServiceURL>
<DataBaseName>WTS</DataBaseName>
<ExpiredMessage>Subscription to this account is expired. Please contact System Administrator</ExpiredMessage>
<ExpirationMessage>Subscription to this account will expire soon. Please contact System Administrator</ExpirationMessage>
<WarningDays>3</WarningDays>
<logo>CLINSH0184.jpg</logo>
<LicenseDetails>
<row>
<ItemCode>SaaS-CRM-Sales</ItemCode>
<ItemName>SaaS - CRM Module-Sales</ItemName>
<StartDate>2014-07-15T00:00:00</StartDate>
<EndDate>2014-08-15T00:00:00</EndDate>
<License>1</License>
</row>
<row>
<ItemCode>SaaS-CRM-Purchase</ItemCode>
<ItemName>SaaS - CRM Module-Purchase</ItemName>
<StartDate>2014-07-15T00:00:00</StartDate>
<EndDate>2014-08-15T00:00:00</EndDate>
<License>2</License>
</row>
<row>
<ItemCode>SaaS-CRM-Service</ItemCode>
<ItemName>SaaS - CRM Module-Service</ItemName>
<StartDate>2014-07-15T00:00:00</StartDate>
<EndDate>2014-08-15T00:00:00</EndDate>
<License>3</License>
</row>
</LicenseDetails>
</row>
</Tenants>
</Data>
在提及的xml中,我使用下面的代码来获取xml标签的值。
var bindName='Response Data Tenants row StartDate';
$xmlNode = $xml.find(bindName);
if ($xmlNode != null) {
var value = $xmlNode.text();
//do some thing with code.
//here I am geting all value of xml tag start with 'StartDate'
//I am expecting value of only single node specified in bindName variable.
}
有人可以在这方面帮助我吗?
最佳答案
如果您仅在 row
节点中查找 StartDate
,则应使用直接子选择器而不是后代选择器。您的最终查询选择器应该是:
var bindName='响应数据租户>行>开始日期';
这将从 LicenseDetails
部分中排除 StartDate
。
请记住,如果您有多个 Tenants
或单个 Tenant
内有多个 row
部分,您仍然可能会得到多个结果,并且应该使用 .each()
对其进行迭代:
$xmlNode = $xml.find(bindName);
if ($xmlNode.length > 0) {
$xmlNode.each(function(index, item) {
console.log($(this).text());
});
}
关于javascript - 在 jQuery 中使用 find 方法获取多个标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404419/