0

如何在不考虑作为其子节点的“”节点的情况下遍历节点。

使用下面的示例代码,我得到 9 个循环。但我应该得到 3 个循环。

NodeList TravellerInfo=BookingresponseEle.getElementsByTagName("TravellerInfo");

元素 TravellerInfoEle=(元素)TravellerInfo.item(0);

NodeList Items=TravellerInfoEle.getElementsByTagName("Item");

XML在下面

<TravellerInfo>        
<Item>          
    <TicketNumber>5899553184513</TicketNumber>          
    <PaxType>ADT</PaxType>          
    <Title> MR</Title>          
    <FirstName>TEST</FirstName>          
    <LastName>TEST</LastName>          
    <DateOfBirth />          
    <SegmentInformation>            
    <Item>             
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184513</TicketNo>              
        <FlightNumber>2777</FlightNumber>              
        <Origin>BLR</Origin>              
        <Destination>VTZ</Destination>              
        <DepartureDateTime>24/07/2012 06:50</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 08:55</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>            
    <Item>              
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184513</TicketNo>              
        <FlightNumber>2704</FlightNumber>              
        <Origin>VTZ</Origin>              
        <Destination>MAA</Destination>              
        <DepartureDateTime>24/07/2012 19:15</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 21:00</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>  
    </SegmentInformation>        
 </Item>
 <Item>          
    <TicketNumber>5899553184512</TicketNumber>          
    <PaxType>CHD</PaxType>          
    <Title> MSTR</Title>          
    <FirstName>TESTC</FirstName>          
    <LastName>TESTC</LastName>          
    <DateOfBirth>15/07/2009</DateOfBirth>          
    <SegmentInformation>            
    <Item>              
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184512</TicketNo>              
        <FlightNumber>2777</FlightNumber>              
        <Origin>BLR</Origin>              
        <Destination>VTZ</Destination>              
        <DepartureDateTime>24/07/2012 06:50</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 08:55</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>            
    <Item>              
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184512</TicketNo>              
        <FlightNumber>2704</FlightNumber>              
        <Origin>VTZ</Origin>              
        <Destination>MAA</Destination>              
        <DepartureDateTime>24/07/2012 19:15</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 21:00</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>            
    </SegmentInformation>        
    </Item>
          <Item>          
    <TicketNumber>5899553184514</TicketNumber>          
    <PaxType>ADT</PaxType>          
    <Title> MR</Title>          
    <FirstName>TESTT</FirstName>          
    <LastName>TESTT</LastName>          
    <DateOfBirth />          
    <SegmentInformation>            
    <Item>             
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184514</TicketNo>              
        <FlightNumber>2777</FlightNumber>              
        <Origin>BLR</Origin>              
        <Destination>VTZ</Destination>              
        <DepartureDateTime>24/07/2012 06:50</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 08:55</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>            
    <Item>              
        <AirlinePNR>MWVGUH</AirlinePNR>              
        <TicketNo>5899553184514</TicketNo>              
        <FlightNumber>2704</FlightNumber>              
        <Origin>VTZ</Origin>              
        <Destination>MAA</Destination>              
        <DepartureDateTime>24/07/2012 19:15</DepartureDateTime>              
        <ArrivalDateTime>24/07/2012 21:00</ArrivalDateTime>              
        <AirCraftType>AT7</AirCraftType>              
        <CarrierCode>9W</CarrierCode>              
        <ClassCode>W</ClassCode>              
        <FareBasis>W2SA14JK</FareBasis>              
        <FrequentFlyerNumber />              
        <SpRequest />              
        <MealsPreference>VGML meals status : UN</MealsPreference>            
    </Item>            
    </SegmentInformation>        
</Item>      
  </TravellerInfo>  
4

1 回答 1

0

我认为你应该看看getChildNodes()。这将为您提供调用它的节点的直接后代。

然后,您应该检查结果中的每个节点NodeList以确定<Item>

于 2012-07-06T09:31:25.193 回答