2014-10-16 3 views
0

.find 메소드를 사용하여 xml 태그의 값을 가져옵니다. xmltag가 반복되면 내 XML에서 모든 태그 값을 제공합니다. 예 언급 XML의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 태그의 값을 얻기 위해 코드 아래 사용합니다.

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을 제외 것이다.

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

덕분에 데릭, 문제 해결 :

단일 Tenant 내부에 여러 Tenants 또는 여러 row 섹션이있는 경우 당신은 여전히 ​​하나 개 이상의 결과를 얻을 수 있으며,이 .each()를 사용하여 반복해야 있음을 유의하십시오. – Manish

1

원하는 행을 지정해야합니다. 이제 모든 행에서 startdate와 일치합니다.

변경

var bindName='Response Data Tenants row StartDate'; 

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

에 첫 번째 행하였습니다.

업데이트 죄송합니다. 상위 레벨 행 요소도 발견되어 업데이트되었습니다.

관련 문제