dom4j解析Xml,dom4j解析带命名空间的Xml内容,dom4j解析xml为实体类

首先引入maven:

        <dependency>
            <groupId>dom4jgroupId>
            <artifactId>dom4jartifactId>
            <version>1.6.1version>
        dependency>

        <dependency>
            <groupId>jaxengroupId>
            <artifactId>jaxenartifactId>
            <version>1.1.6version>
        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>SUCCESSHighestSeverity>
            <CompletedTrackDetails>
                <HighestSeverity>SUCCESSHighestSeverity>
                <Notifications>
                    <Severity>SUCCESSSeverity>
                    <Source>trckSource>
                    <Code>0Code>
                    <Message>Request was successfully processed.Message>
                    <LocalizedMessage>Request was successfully processed.LocalizedMessage>
                Notifications>
                <DuplicateWaybill>falseDuplicateWaybill>
                <MoreData>falseMoreData>
                <TrackDetailsCount>0TrackDetailsCount>
                <TrackDetails>
                    <Notification>
                        <Severity>SUCCESSSeverity>
                        <Source>trckSource>
                        <Code>0Code>
                        <Message>Request was successfully processed.Message>
                        <LocalizedMessage>Request was successfully processed.LocalizedMessage>
                    Notification>
                    <Events>
                        <Timestamp>2017-08-09T09:29:00+02:00Timestamp>
                        <EventType>DLEventType>
                        <EventDescription>Delivered to a non-FedEx clearance brokerEventDescription>
                        <Address>
                            <City>CAMPEGINECity>
                            <StateOrProvinceCode>REStateOrProvinceCode>
                            <PostalCode>42040PostalCode>
                            <CountryCode>ITCountryCode>
                            <CountryName>ItalyCountryName>
                            <Residential>falseResidential>
                        Address>
                        <ArrivalLocation>DELIVERY_LOCATIONArrivalLocation>
                    Events>
                    <Events>
                        <Timestamp>2017-08-09T08:58:00+02:00Timestamp>
                        <EventType>ODEventType>
                        <EventDescription>On FedEx vehicle for deliveryEventDescription>
                        <Address>
                            <City>CAMPEGINECity>
                            <PostalCode>42040PostalCode>
                            <CountryCode>ITCountryCode>
                            <CountryName>ItalyCountryName>
                            <Residential>falseResidential>
                        Address>
                        <ArrivalLocation>VEHICLEArrivalLocation>
                    Events>
                    <Events>
                        <Timestamp>2017-08-09T08:39:00+02:00Timestamp>
                        <EventType>AREventType>
                        <EventDescription>At local FedEx facilityEventDescription>
                        <Address>
                            <City>CAMPEGINECity>
                            <PostalCode>42040PostalCode>
                            <CountryCode>ITCountryCode>
                            <CountryName>ItalyCountryName>
                            <Residential>falseResidential>
                        Address>
                        <ArrivalLocation>DESTINATION_FEDEX_FACILITYArrivalLocation>
                    Events>
                TrackDetails>
            CompletedTrackDetails>
        TrackReply>
    SOAP-ENV:Body>
SOAP-ENV:Envelope>

 

java 代码解析带命名空间内的Events标签内容:

private List getFeddeDetails(String body) {
        List eventList = new ArrayList();
        Document document;
        try {
            Map 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 EventsList = xPath.selectNodes(element);
            Iterator EventsIter = EventsList.iterator();
            while (EventsIter.hasNext()) {
                Element eventElt = EventsIter.next();
                Event event = new Event();
                Iterator 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]

 

转载于:https://www.cnblogs.com/Garen/p/7422397.html

你可能感兴趣的:(java)