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

首先引入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]

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值