2

感谢您考虑这个问题。

我正在阅读一个复杂的 XML 文件,正如您在代码中看到的那样,它有 44 个主要“节点”。每个节点都有进一步的嵌套元素等等。

我已经设法从第一个节点读取信息,但似乎在第一次迭代之后,只返回 null。我会错过什么?

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");
        }

我正在阅读的 XML 如下:

<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

1 回答 1

8

您最好使用JAXB:使用 xsd 文件,您将能够生成代表您的模型的 java 类,而不必编写所有这些数据提取代码。

于 2013-07-22T19:31:07.353 回答