2

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>
4
  • Great! So much help! Thats why I love this kind of community! Commented Jul 22, 2013 at 19:23
  • 1
    @AndrewThompson - if I'd just dumped that much XML and heavily indented code in a question I'd thank anyone prepared to consider looking at it ;-) Nothing wrong with being polite Commented Jul 22, 2013 at 19:30
  • @AndrewThompson: Chill out. Commented Jul 22, 2013 at 19:33
  • @earcam I didn't look that far. ;) Commented Jul 22, 2013 at 19:33

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Using it right now. I will have to re-write the entire code to extract it but it's working ok so far. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.