chntgomez
chntgomez

Reputation: 2119

Reading a complex XML Java

Thanks for considering this question.

I'm reading a complex XML file, which as you can see in the code has 44 main "nodes". Each node has further nested elements and so on.

I have managed to read the info from the first node but it seems that after the first iteration, returns only null. What could I be missing?

for (int i=0; i<nodeList.getLength(); i++){
                log(String.valueOf(i));
                Element flightInfo = (Element)nodeList.item(i);
                    NodeList flights = flightInfo.getElementsByTagName("flight");
                        Element flight = (Element)flights.item(0);
                            String flightId = flight.getAttribute("id");
                            String airlineCode = flight.getAttribute("airlineCode");
                            String operationType = flight.getAttribute("operationType");
                            String flightRoute= flight.getAttribute("flightType");
                            String scheduledTime = flight.getAttribute("scheduledTime");
                            NodeList routingList = flight.getElementsByTagName("routingList");
                                Element iatas = (Element)routingList.item(0);
                                    NodeList _iata = (iatas.getElementsByTagName("IATA"));
                                        String iata = _iata.item(i).getFirstChild().getNodeValue();

                            NodeList times = flight.getElementsByTagName("times");
                                Element realTimes = (Element)times.item(0);
                                    NodeList _realTime = (realTimes.getElementsByTagName("realTime"));
                                        String realTime = _realTime.item(0).getFirstChild().getNodeValue();
                            NodeList means = flight.getElementsByTagName("means");
                                Element gates = (Element)means.item(0);
                                    NodeList _gate = gates.getElementsByTagName("gate");
                                        Element gate = (Element)_gate.item(0);
                                            String gateId = gate.getAttribute("id");
                                Element bagClaimList = (Element)means.item(0);
                                    NodeList bagClaims = bagClaimList.getElementsByTagName("bagClaim");
                                        Element bagClaim = (Element)bagClaims.item(0);
                                            String bagId = bagClaim.getAttribute("id");
                                Element standList = (Element)means.item(0);
                                    NodeList stands = standList.getElementsByTagName("stand");
                                        Element _stand = (Element)stands.item(i);
                                            String standId = _stand.getAttribute("id");

                            NodeList remarks = flight.getElementsByTagName("flight");
                                Element remarkCodes = (Element)remarks.item(0);
                                    NodeList _remarkCode = (remarkCodes.getElementsByTagName("remarkCode"));
                                    String remarkCode = _remarkCode.item(0).getFirstChild().getNodeValue();
                flightList.add(new Flight(flightId, airlineCode, operationType,iata, scheduledTime, iata, realTime, gateId, bagId, standId, remarkCode));
                log("Added new flightInfo");
        }

The XML I'm reading is the following:

<flightData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://c:/SITA/IKUSI FIDS/FIDS.XSD">
<flightInfo>
<flight id="AM2613" airlineCode="AM" flightNumber="2613" operationType="D" flightType="D" scheduledTime="2013-07-18T07:00:00">
<routingList>
<IATA>MTY</IATA>
</routingList>
<times>
<realTime>2013-07-18T07:00:00</realTime>
</times>
<means>
<gateList>
<gate id="N/14"/>
</gateList>
<bagClaimList>
<bagClaim id="2"/>
</bagClaimList>
<standList>
<stand id="5"/>
</standList>
</means>
<remarks>
<remarkCode>DEP</remarkCode>
</remarks>
</flight>
</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>
<flightInfo>...</flightInfo>

Upvotes: 2

Views: 1290

Answers (1)

Guillaume
Guillaume

Reputation: 14671

You'd be better off using JAXB: with the xsd file you will be able to generate java classes representing your model and won't have to write all this data extraction code.

Upvotes: 8

Related Questions