首先引入maven:
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
要解析的 xml 内容:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmladnsoaper.org/soaper/envel/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <TrackReply xmlns="http://fedde.com/ws/track/v14"> <HighestSeverity>SUCCESS</HighestSeverity> <CompletedTrackDetails> <HighestSeverity>SUCCESS</HighestSeverity> <Notifications> <Severity>SUCCESS</Severity> <Source>trck</Source> <Code>0</Code> <Message>Request was successfully processed.</Message> <LocalizedMessage>Request was successfully processed.</LocalizedMessage> </Notifications> <DuplicateWaybill>false</DuplicateWaybill> <MoreData>false</MoreData> <TrackDetailsCount>0</TrackDetailsCount> <TrackDetails> <Notification> <Severity>SUCCESS</Severity> <Source>trck</Source> <Code>0</Code> <Message>Request was successfully processed.</Message> <LocalizedMessage>Request was successfully processed.</LocalizedMessage> </Notification> <Events> <Timestamp>2017-08-09T09:29:00+02:00</Timestamp> <EventType>DL</EventType> <EventDescription>Delivered to a non-FedEx clearance broker</EventDescription> <Address> <City>CAMPEGINE</City> <StateOrProvinceCode>RE</StateOrProvinceCode> <PostalCode>42040</PostalCode> <CountryCode>IT</CountryCode> <CountryName>Italy</CountryName> <Residential>false</Residential> </Address> <ArrivalLocation>DELIVERY_LOCATION</ArrivalLocation> </Events> <Events> <Timestamp>2017-08-09T08:58:00+02:00</Timestamp> <EventType>OD</EventType> <EventDescription>On FedEx vehicle for delivery</EventDescription> <Address> <City>CAMPEGINE</City> <PostalCode>42040</PostalCode> <CountryCode>IT</CountryCode> <CountryName>Italy</CountryName> <Residential>false</Residential> </Address> <ArrivalLocation>VEHICLE</ArrivalLocation> </Events> <Events> <Timestamp>2017-08-09T08:39:00+02:00</Timestamp> <EventType>AR</EventType> <EventDescription>At local FedEx facility</EventDescription> <Address> <City>CAMPEGINE</City> <PostalCode>42040</PostalCode> <CountryCode>IT</CountryCode> <CountryName>Italy</CountryName> <Residential>false</Residential> </Address> <ArrivalLocation>DESTINATION_FEDEX_FACILITY</ArrivalLocation> </Events> </TrackDetails> </CompletedTrackDetails> </TrackReply> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
java 代码解析带命名空间内的Events标签内容:
private List<Event> getFeddeDetails(String body) { List<Event> eventList = new ArrayList<Event>(); Document document; try { Map<String, String> nameSpaceMap = new HashMap<>(); nameSpaceMap.put("track", "http://Fedde.com/waws11/track12/v14"); document = DocumentHelper.parseText(body); Element element = document.getRootElement(); XPath xPath = element.createXPath("//track:Events"); xPath.setNamespaceURIs(nameSpaceMap); List<Element> EventsList = xPath.selectNodes(element); Iterator<Element> EventsIter = EventsList.iterator(); while (EventsIter.hasNext()) { Element eventElt = EventsIter.next(); Event event = new Event(); Iterator<Element> subIter = eventElt.elements().iterator(); while (subIter.hasNext()) { Element subElt = subIter.next(); if ("Timestamp".equals(subElt.getName())) { event = dateFormat(subElt.getStringValue(), event); } else if ("EventDescription".equals(subElt.getName())) { event.setDescription(subElt.getStringValue()); } else if ("Address".equals(subElt.getName())) { Element cityElt = subElt.element("City"); event.setCity(cityElt.getStringValue()); Element CountryElt = subElt.element("CountryName"); event.setCountry(CountryElt.getStringValue()); } } eventList.add(event); } for (Event event : eventList) { System.out.println(event); } } catch (DocumentException | ParseException e) { e.printStackTrace(); } return eventList; }
输出结果:
Event [city=KWUN TONG, country=Hong Kong, date=2017-08-07, time=12:10, description=Left FedEx origin facility] Event [city=KWUN TONG, country=Hong Kong, date=2017-08-07, time=09:01, description=Picked up]
时间: 2024-11-07 11:16:34