2014-10-16 79 views
0

我正在使用.find方法來獲取xml標記的值。在我的xml中,如果xmltag重複,它會給我所有標記的值。 例在jQuery中使用find方法時獲取多個標記值

<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>[email protected]</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. 
} 

有人可以在這方面幫助我嗎?

回答

1

如果您只查找row節點中的StartDate,則應該使用直接子選擇器而不是後代選擇器。您最終的查詢選擇應該是:

var bindName='Response Data Tenants > row > StartDate';

這將排除在LicenseDetails部分的StartDate

請記住,如果你有一個Tenant內的多個Tenants或多個row部分,你還可以得到一個以上的結果,應該重複使用它在.each()

$xmlNode = $xml.find(bindName); 
if ($xmlNode.length > 0) { 
    $xmlNode.each(function(index, item) { 
     console.log($(this).text()); 
    }); 
} 
+0

由於德里克,問題解決了。 – Manish 2014-10-17 04:40:10

1

您需要指定所需的行,現在它匹配每行中的startdate。

變化

var bindName='Response Data Tenants row StartDate'; 

var bindName='Response Data Tenants row row:first StartDate'; 

第一行相匹配。

更新 對不起,更新,因爲我也找到了上層行元素。