如何遍历<travellerinfo>中的<item>节点而不考虑作为其子节点的“<item>”节点。</item> </travellerinfo> </item>

时间:2012-07-06 09:16:59

标签: java xml-parsing

如何在不考虑&#34;&#34;的情况下遍历节点。节点,它是它的子节点。

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

NodeList TravellerInfo = BookingresponseEle.getElementsByTagName(&#34; TravellerInfo&#34;);

Element TravellerInfoEle =(Element)TravellerInfo.item(0);

NodeList Items = TravellerInfoEle.getElementsByTagName(&#34; Item&#34;);

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>  

1 个答案:

答案 0 :(得分:0)

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

然后,您应检查结果NodeList中的每个节点,以确定它是<Item>(您最有可能在元素之间获取文本节点等。因此检查至关重要 - 请参阅{{3} }以及更多信息的问题)