java dom4j封装和解析XML
package org.scbit.lsbi.scp.utils; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.scbit.lsbi.scp.utils.enums.TemplateDeviceEnum; import org.scbit.lsbi.scp.vo.TemplateDeviceFormVo; import org.scbit.lsbi.scp.vo.TemplateDeviceVo; public class CurrentXMLUtil { // 封装数据到XML public static String createXML(TemplateDeviceFormVo templateDeviceFormVo) { List<TemplateDeviceVo> templateDeviceVo = templateDeviceFormVo.getTemplateDeviceVos(); Document doc = DocumentHelper.createDocument(); doc.setXMLEncoding("UTF-8"); Element root = doc.addElement("custom"); for (int i = 0; i < templateDeviceVo.size(); i++) { Element iterm = root.addElement("iterm"); iterm.addAttribute("filed", templateDeviceVo.get(i).getParamId()); iterm.addElement("name").setText(templateDeviceVo.get(i).getFiledName()); iterm.addElement("code") .setText(ChineseInitials.getPinYinHeadChar(templateDeviceVo.get(i).getFiledName(), 1)); String typ = TemplateDeviceEnum.getCnName(templateDeviceVo.get(i).getType()); Element type = iterm.addElement("type").addAttribute("value", typ); if ("radio".equals(templateDeviceVo.get(i).getType()) || "select".equals(templateDeviceVo.get(i).getType())) { if (templateDeviceVo.get(i).getOptions() != null) for (int j = 0; j < templateDeviceVo.get(i).getOptions().size(); j++) { type.addElement("option").setText(templateDeviceVo.get(i).getOptions().get(j)); } if (StringUtil.hasText(templateDeviceVo.get(i).getSelect())) { iterm.addElement("normalValue").setText(templateDeviceVo.get(i).getOptions() .get(Integer.parseInt(templateDeviceVo.get(i).getSelect().length() + "") - 2)); } } if ("number".equals(templateDeviceVo.get(i).getType()) && StringUtil.hasText(templateDeviceVo.get(i).getLocalUnit()) && !StringUtil.hasText(templateDeviceVo.get(i).getMaxNum())) { iterm.addElement("meunit").setText(templateDeviceVo.get(i).getLocalUnit()); iterm.addElement("number").setText(templateDeviceVo.get(i).getSignVal()); iterm.addElement("sign").setText(templateDeviceVo.get(i).getSign());// templateDeviceVo.get(i).getSign(); } if ("number_value".equals(templateDeviceVo.get(i).getType()) && StringUtil.hasText(templateDeviceVo.get(i).getMaxNum()) && StringUtil.hasText(templateDeviceVo.get(i).getRangeUnit())) { iterm.addElement("meunit").setText(templateDeviceVo.get(i).getRangeUnit()); iterm.addElement("min").setText(templateDeviceVo.get(i).getMinNum()); iterm.addElement("max").setText(templateDeviceVo.get(i).getMaxNum()); } if ("yin_yang".equals(templateDeviceVo.get(i).getType())) { if (templateDeviceVo.get(i).getOptions() != null) { for (int j = 0; j < templateDeviceVo.get(i).getOptions().size(); j++) { type.addElement("option").setText(templateDeviceVo.get(i).getOptions().get(j)); } } if (StringUtil.hasText(templateDeviceVo.get(i).getSelect())) { iterm.addElement("normalValue").setText(templateDeviceVo.get(i).getSelect()); } } } return doc.asXML(); } public static TemplateDeviceFormVo parseXML(String xml) { Document doc = null; Element root = null; TemplateDeviceFormVo templateDeviceFormVo = new TemplateDeviceFormVo(); try { doc = DocumentHelper.parseText(xml); root = doc.getRootElement(); List<Element> elements = root.elements(); List<TemplateDeviceVo> templateDeviceVos = new ArrayList<>(); for (int i = 0; i < elements.size(); i++) { TemplateDeviceVo templateDeviceVo = new TemplateDeviceVo(); Element iterm = elements.get(i); String name = iterm.elementText("name"); String code = iterm.elementText("code"); String type = iterm.element("type").attribute("value").getText(); String paramId = iterm.attribute("filed").getText(); templateDeviceVo.setParamId(paramId); templateDeviceVo.setFiledName(name); templateDeviceVo.setCode(code); templateDeviceVo.setType(type); if (iterm.element("type").hasContent()) { List<Element> options = iterm.element("type").elements("option"); List<String> ops = new ArrayList<>(); for (Element option : options) { String opText = option.getText(); ops.add(opText); } templateDeviceVo.setOptions(ops); } if (iterm.element("normalValue") != null) { templateDeviceVo.setSelect(iterm.element("normalValue").getText()); } if (iterm.element("meunit") != null && iterm.element("max") == null) { templateDeviceVo.setLocalUnit(iterm.element("meunit").getText()); templateDeviceVo.setSignVal(iterm.element("number").getText()); templateDeviceVo.setSign(iterm.element("sign").getText()); } if (iterm.element("meunit") != null && iterm.element("max") != null) { templateDeviceVo.setRangeUnit(iterm.element("meunit").getText()); templateDeviceVo.setMinNum(iterm.element("min").getText()); templateDeviceVo.setMaxNum(iterm.element("max").getText()); } templateDeviceVos.add(templateDeviceVo); } templateDeviceFormVo.setTemplateDeviceVos(templateDeviceVos); } catch (DocumentException e) { e.printStackTrace(); } return templateDeviceFormVo; } }
maven包:
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
XML示例:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <custom> 3 <iterm filed=""> 4 <name>白细胞数</name> 5 <code>bxb</code> 6 <type value="数字"></type> 7 <meunit>个</meunit> 8 <min>4</min> 9 <max>10</max> 10 </iterm> 11 <iterm filed=""> 12 <name>白细胞数</name> 13 <code>bxb</code> 14 <type value="数字"></type> 15 <meunit>个</meunit> 16 <number>20</number> 17 </iterm> 18 <iterm filed=""> 19 <name>细菌阴阳性</name> 20 <code>zc</code> 21 <type value="单选框"> 22 <option >阴</option> 23 <option>阳</option> 24 </type> 25 <required>false</required> 26 <normalValue>阴</normalValue> 27 </iterm> 28 <iterm filed=""> 29 <name>照片</name> 30 <code>tp</code> 31 <type value="图片"></type> 32 <required>false</required> 33 </iterm> 34 <iterm filed=""> 35 <name>描述</name> 36 <code>ms</code> 37 <type value="文本框"></type> 38 <required>false</required> 39 </iterm> 40 </custom>