Dom4j:XML生成与解析实例详解

2星 需积分: 31 4 下载量 15 浏览量 更新于2024-09-12 收藏 43KB DOC 举报
Dom4j是一个强大的Java库,用于处理XML文档的生成和解析。在这个教程中,我们将学习如何使用Dom4j来创建XML文件,并且展示如何解析这些XML数据。首先,我们了解Dom4j的基本概念,它提供了一种面向对象的方式来操作XML文档。 在Java中,利用Dom4j生成XML的主要步骤如下: 1. 引入依赖:你需要在项目中引入Dom4j库,可以通过Maven或Gradle进行添加,例如: ```xml <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> ``` 2. 创建XML文档:通过`DocumentHelper`类创建一个新的XML文档对象。在`testGenXml`方法中,首先定义命名空间(namespace)并设置编码格式: ```java String namespace = "urn:3GPP:metadata:2005:mTV:BSFaddrResponse"; Document doc = DocumentHelper.createDocument(); ``` 3. 创建根元素:在`doc`对象上添加根元素`Root`,同时指定两个命名空间: ```java Element root = doc.addElement("Root"); Namespace namespace1 = new DefaultNamespace("xs", "http://www.w3.org/2001/XMLSchema"); Namespace namespace2 = new DefaultNamespace("", namespace); root.add(namespace1); root.add(namespace2); ``` 4. 添加子元素和文本:在根元素下创建子元素`BSF_ADDRESS`,并添加文本内容: ```java Element bsfAdress = root.addElement("BSF_ADDRESS", namespace); bsfAdress.addText("testaddress"); ``` 5. 处理命名空间:获取并打印子元素的命名空间,然后移除不需要的命名空间: ```java Namespace testSpace = bsfAdress.getNamespace(); System.out.println(testSpace.getStringValue()); bsfAdress.remove(namespace2); ``` 6. 设置输出格式:使用`OutputFormat`来控制输出的XML格式,如无缩进,然后创建`XMLWriter`对象进行写入: ```java OutputFormat format = new OutputFormat("", true); XMLWriter writer = new XMLWriter(System.out, format); writer.write(doc); ``` 7. 解析XML:在Dom4j中,你可以使用`Document`对象的API来解析XML数据。解析XML示例代码未给出,但通常会涉及使用`Element`对象遍历节点树,提取所需的数据。 总结来说,Dom4j简化了XML文档的生成和操作,通过创建`Document`、`Element`等对象,可以方便地构建复杂的XML结构。同时,其提供了丰富的API支持,使得XML的读取、修改和解析变得高效易行。通过这个例子,你掌握了基本的Dom4j操作技巧,进一步探索可以深入了解其他高级功能,如XPath查询、事件驱动解析等。
2013-05-05 上传
该项目采用dom4j从数据库表中生成xml数据 项目文档、数据库建表语句均已放置在项目中。 关键代码如下: public String getXml(Connection conn, int rm_id, String path) { //声明xml字符串 String fileString = ""; //创建DAO对象 MachineroomDao machineroom_dao = new MachineroomDao(); Cab_equipmentDao cab_equipment_dao = new Cab_equipmentDao(); Equip_configDao equip_config_dao = new Equip_configDao(); EquipmentDao equipment_dao = new EquipmentDao(); //添加room,第一层 Machineroom machineroom_dto = machineroom_dao.findById(conn, rm_id);//设置房间号 //获取个属性的值.如果为null,将属性设为"" String getMr_id = new Integer(machineroom_dto.getMr_id()).toString(); String getMr_name = machineroom_dto.getMr_name(); if(getMr_id == null) getMr_id = ""; if(getMr_name == null) getMr_name = ""; Document document = DocumentHelper.createDocument(); Element rooms_racks = document.addElement("rooms-racks"); Element room = rooms_racks.addElement("room"); room.addAttribute("id", getMr_id); room.addAttribute("name", getMr_name); room.addAttribute("isSelected", "true"); //添加rack,第二层 List<Cab_equipment> cab_equipment_list = cab_equipment_dao.findById(conn, path, machineroom_dto.getMr_id()); for (int i = 0; i < cab_equipment_list.size(); i++) { Cab_equipment cab_equipment_dto = cab_equipment_list.get(i); //获取个属性的值.如果为null,将属性设为"" String getE_id = cab_equipment_dto.getE_id(); String getEqucab_name = cab_equipment_dto.getEqucab_name(); String getX = cab_equipment_dto.getX() + ""; String getY = cab_equipment_dto.getY() + ""; String getZ = cab_equipment_dto.getZ() + ""; String getLongs = cab_equipment_dto.getLongs() + ""; String getWidth = cab_equipment_dto.getWidth() + ""; String getHighs = cab_equipment_dto.getHighs() + ""; String getRotate_angle = cab_equipment_dto.getRotate_angle(); if(getE_id == null) getE_id = ""; if(getEqucab_name == null) getEqucab_name = ""; if(getRotate_angle == null) getRotate_angle = ""; //将float后的".0"去掉 if(cab_equipment_dto.getX()%1 == 0) getX = (int)cab_equipment_dto.getX()+""; if(cab_equipment_dto.getY()%1 == 0) getY = (int)cab_equipment_dto.getY()+""; if(cab_equipment_dto.getZ()%1 == 0) getZ = (int)cab_equipment_dto.getZ()+""; if(cab_equipment_dto.getHighs()%1 == 0) getHighs = (int)cab_equipment_dto.getHighs()+""; Element rack = room.addElement("rack"); rack.addAttribute("id", getE_id); rack.addAttribute("name", getEqucab_name); rack.addAttribute("x", getX); rack.addAttribute("y", getY); rack.addAttribute("z", getZ); rack.addAttribute("lengthX", getLongs); rack.addAttribute("lengthY", getWidth); rack.addAttribute("lengthZ", getHighs); rack.addAttribute("rotation", getRotate_angle); //添加device,第三层 int u = 0;//表示需要计算的u高,其xml的属性为uplace int count = 0;//计数器,临时变量 int count2 = 0; List<Equip_config> equip_config_list = equip_config_dao.findByLocation(conn, cab_equipment_dto.getE_id()); for (int j = 0; j < equip_config_list.size(); j++) { Equip_config equip_config_dto = equip_config_list.get(j); Equipment equipment_dto = equipment_dao.findBySerial(conn, equip_config_dto.getSerial()); //获取个属性的值.如果为null,将属性设为"" String getSerial = equip_config_dto.getSerial(); String getEquipmentname = equip_config_dto.getEquipmentname(); String getEq_typecn = equipment_dto.getEq_typecn(); String getImagepath = equip_config_dto.getImagepath(); String getStorey = equip_config_dto.getStorey(); //String getU = equip_config_dto.getU() + ""; if(getSerial == null) getSerial = ""; if(getEquipmentname == null) getEquipmentname = ""; if(getEq_typecn == null) getEq_typecn = ""; if(getImagepath == null) getImagepath = ""; if(getStorey == null) getStorey = ""; int getU = equip_config_dto.getU(); //处理u高 if(getU != 0){ count++; } if(count2==0){ if(count == 1){ u = 2; } }else{ Equip_config equip_config_dto_temp = equip_config_list.get(count2 - 1); Equipment equipment_dto_temp = equipment_dao.findBySerial(conn, equip_config_dto_temp.getSerial()); int getU_temp = equip_config_dto_temp.getU(); if(count == 1){ u = 2; }else { if(getU_temp == 0){ u = u + getU_temp/10 + 0; } else { if(getU_temp == 0){ u = u + getU_temp/10 +2; }else { u = u + getU_temp/10 + 1 + 2; } } } } count2++; String uplace = u + ""; String GetU = getU + ""; if(getU == 0) { uplace = "0"; }//u高处理完毕 Element device = rack.addElement("device"); device.addAttribute("num", getSerial); device.addAttribute("name", getEquipmentname); device.addAttribute("type", getEq_typecn); device.addAttribute("image", getImagepath); //device.addAttribute("storey", getStorey);//设备所在的层数,该属性不在xml中展示 device.addAttribute("uplace", uplace);//计算得来的u高 device.addAttribute("uheight", GetU);//图片的高度 } } //设置xml输出格式 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); StringWriter out = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(out, format); try { xmlWriter.write(document); fileString = out.toString(); xmlWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileString; }