Skip to main content
 首页 » 编程设计

java使用dom4j解析xml

2022年07月18日28kerrycode

待解析的字符串:

<faultduty>
<dworkerfaultduty dutyname="K" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="W" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="采购部" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="技术工程部" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="技术中心" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="事业部" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="物流中心" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="责任单位" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="转向架" username="" userphone="" card=""/>
<dworkerfaultduty dutyname="安庆三维" username="张婧1" userphone="176858941461" card="3702831987112458691" userid="e2a979fb-fbd8-4fd1-b4c9-c31f72cc8dfc"/>
<dworkerfaultduty dutyname="安庆三维" username="章1" userphone="17689862546" card="123456" userid="224c5167-bb91-4b12-b93a-333984a847f5"/>
<dworkerfaultduty dutyname="航测205-魏小凯" username="李帅" userphone="18669838241" card="375892423687132498" userid="95e50868-d73b-4f04-bab1-19acd9b33743"/>
<dworkerfaultduty dutyname="航测205-魏小凯" username="郑修强" userphone="158954458569" card="12354855445" userid="755fa569-8bb3-48cc-8c62-4b7bd7e3a4b3"/>
<dworkerfaultduty dutyname="航测205" username="张三" userphone="13800138000" card="130283198905281567" userid="46c71970-1aab-45c5-9181-c696b1ba6b66"/>
<dworkerfaultduty dutyname="华高" username="刘方晓" userphone="13021676555" card="370214198602186519" userid="300bf895-acac-4ac4-a9df-c2314d3036c4"/>
<dworkerfaultduty dutyname="华高" username="耿鹏" userphone="15589869802" card="370281198702050037" userid="4f7aec43-5f48-4e27-afa1-75e28541e6f0"/>
<dworkerfaultduty dutyname="华高" username="张建辉" userphone="17731530159" card="130283199808250611" userid="252db601-266d-4ef4-88b3-a1d790b37348"/>
<dworkerfaultduty dutyname="今创" username="刘德超" userphone="15563445538" card="370214199205085030" userid="b2ead0ed-b4c8-414c-a807-44fcad3efc45"/>
</faultduty>

 第一步:引用jar包:

<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>

第二步:定义转换对象:

package com.rongyi.platform.game.web.controller; 
 
import lombok.Data; 
 
import java.io.Serializable; 
 
/** 
 * @desc: 
 * @author: 毛会懂 
 **/ 
@Data 
public class CodeVO implements Serializable { 
    private String Company; 
 
    private String name; 
 
    private String phone; 
}

第三步:转换代码

package com.rongyi.platform.game.web.controller; 
 
import com.alibaba.fastjson.JSONArray; 
import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.DocumentHelper; 
import org.dom4j.Element; 
 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
 
/** 
 * @desc: 
 * @author: 毛会懂 
 **/ 
public class Xml3Controller { 
 
    public static void main(String[] args) { 
        String str = "\n" + 
                "\n" + 
                "<faultduty>\n" + 
                "<dworkerfaultduty dutyname=\"K\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"W\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"采购部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"技术工程部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"技术中心\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"事业部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"物流中心\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"责任单位\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"转向架\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"安庆三维\" username=\"张婧1\" userphone=\"176858941461\" card=\"3702831987112458691\" userid=\"e2a979fb-fbd8-4fd1-b4c9-c31f72cc8dfc\"/>\n" + 
                "<dworkerfaultduty dutyname=\"安庆三维\" username=\"章1\" userphone=\"17689862546\" card=\"123456\" userid=\"224c5167-bb91-4b12-b93a-333984a847f5\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205-魏小凯\" username=\"李帅\" userphone=\"18669838241\" card=\"375892423687132498\" userid=\"95e50868-d73b-4f04-bab1-19acd9b33743\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205-魏小凯\" username=\"郑修强\" userphone=\"158954458569\" card=\"12354855445\" userid=\"755fa569-8bb3-48cc-8c62-4b7bd7e3a4b3\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205\" username=\"张三\" userphone=\"13800138000\" card=\"130283198905281567\" userid=\"46c71970-1aab-45c5-9181-c696b1ba6b66\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"刘方晓\" userphone=\"13021676555\" card=\"370214198602186519\" userid=\"300bf895-acac-4ac4-a9df-c2314d3036c4\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"耿鹏\" userphone=\"15589869802\" card=\"370281198702050037\" userid=\"4f7aec43-5f48-4e27-afa1-75e28541e6f0\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"张建辉\" userphone=\"17731530159\" card=\"130283199808250611\" userid=\"252db601-266d-4ef4-88b3-a1d790b37348\"/>\n" + 
                "<dworkerfaultduty dutyname=\"今创\" username=\"刘德超\" userphone=\"15563445538\" card=\"370214199205085030\" userid=\"b2ead0ed-b4c8-414c-a807-44fcad3efc45\"/>\n" + 
                "</faultduty>\n" + 
                "\n"; 
        List<CodeVO> list = splitXml(str, "abc"); 
 
        System.out.print(JSONArray.toJSONString(list)); 
    } 
 
    /** 
     *  把元素解析为对象 
     * xmlStr 要解析的xml字符串 
     * @return 
     */ 
    public static List<CodeVO> splitXml(String xmlStr, String company) { 
 
        List<CodeVO> list = new ArrayList<>(); 
        Document document; 
        try { 
            document = DocumentHelper.parseText(xmlStr); 
            Element root = document.getRootElement(); 
            //返回包含子元素的迭代器 
            Iterator it = root.elementIterator(); 
            while(it.hasNext()) { 
                CodeVO vo = new CodeVO(); 
                vo.setCompany(company); 
                Element element = (Element) it.next(); 
                vo.setName(element.attributeValue("username")); 
                vo.setPhone(element.attributeValue("userphone")); 
                list.add(vo); 
            } 
 
        } catch (DocumentException e) { 
            e.printStackTrace(); 
        } 
        return list; 
    } 
}

复杂一点的:

待解析的字符串:

<RESULT code="1" msg=""> <worksheet> <faultinfo>暂无此消息</faultinfo> <faultclassify> <dworkerfaultclassify classifyname="车端连接"/> <dworkerfaultclassify classifyname="车体及车体附件"/> <dworkerfaultclassify classifyname="电气及配电盘"/> <dworkerfaultclassify classifyname="电务车载"/> <dworkerfaultclassify classifyname="辅助供电"/> <dworkerfaultclassify classifyname="高压供电"/> <dworkerfaultclassify classifyname="给水卫生"/> <dworkerfaultclassify classifyname="供风及制动"/> <dworkerfaultclassify classifyname="驾驶设施"/> <dworkerfaultclassify classifyname="空调供暖"/> <dworkerfaultclassify classifyname="门窗系统"/> <dworkerfaultclassify classifyname="内装系统"/> <dworkerfaultclassify classifyname="其他"/> <dworkerfaultclassify classifyname="牵引系统"/> <dworkerfaultclassify classifyname="网络及控制"/> <dworkerfaultclassify classifyname="烟火"/> <dworkerfaultclassify classifyname="影视信息广播"/> <dworkerfaultclassify classifyname="转向架"/> </faultclassify> <faultreason> <dworkerfaultreason reasonname="供方责任"/> <dworkerfaultreason reasonname="公司产品设计"/> <dworkerfaultreason reasonname="公司产品工艺"/> <dworkerfaultreason reasonname="公司产品制造"/> <dworkerfaultreason reasonname="用户提供产品"/> <dworkerfaultreason reasonname="用户责任"/> <dworkerfaultreason reasonname="外部运用"/> <dworkerfaultreason reasonname="其他"/> </faultreason> <faultduty> <dworkerfaultduty dutyname="K" username="" userphone="" card=""/> <dworkerfaultduty dutyname="W" username="" userphone="" card=""/> <dworkerfaultduty dutyname="采购部" username="" userphone="" card=""/> <dworkerfaultduty dutyname="技术工程部" username="" userphone="" card=""/> <dworkerfaultduty dutyname="技术中心" username="" userphone="" card=""/> <dworkerfaultduty dutyname="事业部" username="" userphone="" card=""/> <dworkerfaultduty dutyname="物流中心" username="" userphone="" card=""/> <dworkerfaultduty dutyname="责任单位" username="" userphone="" card=""/> <dworkerfaultduty dutyname="转向架" username="" userphone="" card=""/> <dworkerfaultduty dutyname="安庆三维" username="张婧1" userphone="176858941461" card="3702831987112458691" userid="e2a979fb-fbd8-4fd1-b4c9-c31f72cc8dfc"/> <dworkerfaultduty dutyname="安庆三维" username="章1" userphone="17689862546" card="123456" userid="224c5167-bb91-4b12-b93a-333984a847f5"/> <dworkerfaultduty dutyname="航测205-魏小凯" username="李帅" userphone="18669838241" card="375892423687132498" userid="95e50868-d73b-4f04-bab1-19acd9b33743"/> <dworkerfaultduty dutyname="航测205-魏小凯" username="郑修强" userphone="158954458569" card="12354855445" userid="755fa569-8bb3-48cc-8c62-4b7bd7e3a4b3"/> <dworkerfaultduty dutyname="航测205" username="张三" userphone="13800138000" card="130283198905281567" userid="46c71970-1aab-45c5-9181-c696b1ba6b66"/> <dworkerfaultduty dutyname="华高" username="刘方晓" userphone="13021676555" card="370214198602186519" userid="300bf895-acac-4ac4-a9df-c2314d3036c4"/> <dworkerfaultduty dutyname="华高" username="耿鹏" userphone="15589869802" card="370281198702050037" userid="4f7aec43-5f48-4e27-afa1-75e28541e6f0"/> <dworkerfaultduty dutyname="华高" username="张建辉" userphone="17731530159" card="130283199808250611" userid="252db601-266d-4ef4-88b3-a1d790b37348"/> <dworkerfaultduty dutyname="今创" username="刘德超" userphone="15563445538" card="370214199205085030" userid="b2ead0ed-b4c8-414c-a807-44fcad3efc45"/> </faultduty> </worksheet> </RESULT>

把xml解析为hashMap:

package com.rongyi.platform.game.web.controller; 
 
 
 
import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.DocumentHelper; 
import org.dom4j.Element; 
 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
 
 
/** 
 * @author: 毛会懂 
 **/ 
public class XML2Controller { 
 
    public static void main(String[] args) { 
        HashMap<String, String> hashMap = splitXml("<RESULT code=\"1\" msg=\"\">\n" + 
                "<worksheet>\n" + 
                "<faultinfo>暂无此消息</faultinfo>\n" + 
                "<faultclassify>\n" + 
                "<dworkerfaultclassify classifyname=\"车端连接\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"车体及车体附件\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"电气及配电盘\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"电务车载\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"辅助供电\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"高压供电\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"给水卫生\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"供风及制动\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"驾驶设施\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"空调供暖\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"门窗系统\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"内装系统\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"其他\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"牵引系统\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"网络及控制\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"烟火\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"影视信息广播\"/>\n" + 
                "<dworkerfaultclassify classifyname=\"转向架\"/>\n" + 
                "</faultclassify>\n" + 
                "<faultreason>\n" + 
                "<dworkerfaultreason reasonname=\"供方责任\"/>\n" + 
                "<dworkerfaultreason reasonname=\"公司产品设计\"/>\n" + 
                "<dworkerfaultreason reasonname=\"公司产品工艺\"/>\n" + 
                "<dworkerfaultreason reasonname=\"公司产品制造\"/>\n" + 
                "<dworkerfaultreason reasonname=\"用户提供产品\"/>\n" + 
                "<dworkerfaultreason reasonname=\"用户责任\"/>\n" + 
                "<dworkerfaultreason reasonname=\"外部运用\"/>\n" + 
                "<dworkerfaultreason reasonname=\"其他\"/>\n" + 
                "</faultreason>\n" + 
                "<faultduty>\n" + 
                "<dworkerfaultduty dutyname=\"K\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"W\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"采购部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"技术工程部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"技术中心\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"事业部\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"物流中心\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"责任单位\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"转向架\" username=\"\" userphone=\"\" card=\"\"/>\n" + 
                "<dworkerfaultduty dutyname=\"安庆三维\" username=\"张婧1\" userphone=\"176858941461\" card=\"3702831987112458691\" userid=\"e2a979fb-fbd8-4fd1-b4c9-c31f72cc8dfc\"/>\n" + 
                "<dworkerfaultduty dutyname=\"安庆三维\" username=\"章1\" userphone=\"17689862546\" card=\"123456\" userid=\"224c5167-bb91-4b12-b93a-333984a847f5\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205-魏小凯\" username=\"李帅\" userphone=\"18669838241\" card=\"375892423687132498\" userid=\"95e50868-d73b-4f04-bab1-19acd9b33743\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205-魏小凯\" username=\"郑修强\" userphone=\"158954458569\" card=\"12354855445\" userid=\"755fa569-8bb3-48cc-8c62-4b7bd7e3a4b3\"/>\n" + 
                "<dworkerfaultduty dutyname=\"航测205\" username=\"张三\" userphone=\"13800138000\" card=\"130283198905281567\" userid=\"46c71970-1aab-45c5-9181-c696b1ba6b66\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"刘方晓\" userphone=\"13021676555\" card=\"370214198602186519\" userid=\"300bf895-acac-4ac4-a9df-c2314d3036c4\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"耿鹏\" userphone=\"15589869802\" card=\"370281198702050037\" userid=\"4f7aec43-5f48-4e27-afa1-75e28541e6f0\"/>\n" + 
                "<dworkerfaultduty dutyname=\"华高\" username=\"张建辉\" userphone=\"17731530159\" card=\"130283199808250611\" userid=\"252db601-266d-4ef4-88b3-a1d790b37348\"/>\n" + 
                "<dworkerfaultduty dutyname=\"今创\" username=\"刘德超\" userphone=\"15563445538\" card=\"370214199205085030\" userid=\"b2ead0ed-b4c8-414c-a807-44fcad3efc45\"/>\n" + 
                "</faultduty>\n" + 
                "</worksheet>\n" + 
                "</RESULT>", "华高"); 
 
        System.out.print(hashMap); 
    } 
 
 
 
    /** 
     *  解析xml 
     * xmlStr 要解析的xml字符串 
     * respUnit 厂家名称 
     * @return 
     */ 
    public static HashMap<String, String> splitXml(String xmlStr, String respUnit) { 
        Document document; 
        HashMap<String, String> map = new HashMap<>(); 
        try { 
            document = DocumentHelper.parseText(xmlStr); 
            Element root = document.getRootElement(); 
            //返回包含子元素的迭代器 
            Iterator it = root.elementIterator(); 
            while(it.hasNext()) { 
                Element element = (Element) it.next(); 
                //获取子节点下的子节点faultduty 
                Iterator elementIterator = element.elementIterator("faultduty"); 
                Element element2 = (Element) elementIterator.next(); 
               // List<Element> content = element2.content(); 
                List<Element> elements = element2.elements(); 
                for (Element object : elements) { 
                    //object.attributeValue("username");  // 根据传入的属性名获取属性值 
                    if(respUnit.equals(object.attributeValue("dutyname"))) { 
                        map.put(object.attributeValue("username"), object.attributeValue("userphone")); 
                        System.out.println(object.attributeValue("dutyname") +" ---- "+ object.attributeValue("username") +" ---- "+ object.attributeValue("userphone")); 
                    } 
 
                } 
            } 
 
        } catch (DocumentException e) { 
            e.printStackTrace(); 
        } 
        return map; 
    } 
}

参考文章:http://www.manongjc.com/article/49134.html


本文参考链接:https://www.cnblogs.com/maohuidong/p/16370330.html