阅读复杂的XML Java

时间:2013-07-22 19:17:51

标签: java xml

感谢您考虑这个问题。

我正在阅读一个复杂的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>

1 个答案:

答案 0 :(得分:8)

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