aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to traverse <Item> node in <TravellerInfo> without considering “<Item>” node which is child node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to traverse <Item> node in <TravellerInfo> without considering “<Item>” node which is child node" Watch "How to traverse <Item> node in <TravellerInfo> without considering “<Item>” node which is child node" New topic
Author

How to traverse <Item> node in <TravellerInfo> without considering “<Item>” node which is child node

Dileep Provab
Greenhorn

Joined: May 19, 2012
Posts: 1
With bellow example code i am getting 9 loops. But i should get 3 loops.

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

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

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

XMl is below

<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>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
Exactly why do you expect 3 "loops"?

A more detailed text description of what you want to happen would probably guide you to the solution.

Bill
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
If you use dom and that you use getElementsByTagName() to get to "Item", you can't avoid that outcome as the method basically flatten out the xml and identifies the match disregard the depth of the node in the hierachy, effectively resulting in 9 entries in the NodeList. However, if you use another method getChildNodes() again of dom, effectively that matches only one level downward in the hierachy but the method does not have an overload of locating a specific tag name as child (because, child might be text or element or comment etc...). Other frameworks may have specific ways to make effectively getChildNodes accepting a tag name as argument, but that is not dom per se.

But to arrive at what you want with only a reasonable performance impact you can do this.
[1] using getElementsByTagName()

[2] use getChildNodes()

If the sample reflects faithfully the real document in the aspect of only Item be the child of TravellerInfo, probably [2] would be a better solution. If the sample does not reflect faithfully the strict Item/Item structure, then the condition in [1] would be insufficient and might be difficult to get a reasonably simple conditional for screening. In any case, [2] seems better suite to your need balancing all considerations.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to traverse <Item> node in <TravellerInfo> without considering “<Item>” node which is child node