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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]

 

以上是关于dom4j解析Xml,dom4j解析带命名空间的Xml内容,dom4j解析xml为实体类的主要内容,如果未能解决你的问题,请参考以下文章

带命名空间的XML的dom4j应用<转>

XML 解析

源生API解析XML文档与dom4j解析XML文档

dom4j解析无根节点的xml

200分求解:dom4j 使用XPATH解析。。诡异的问题

dom4j解析xml重复节点