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示例:
<?xml version="1.0" encoding="UTF-8"?>
<custom>
<iterm filed="">
<name>白细胞数</name>
<code>bxb</code>
<type value="数字"></type>
<meunit>个</meunit>
<min>4</min>
<max>10</max>
</iterm>
<iterm filed="">
<name>白细胞数</name>
<code>bxb</code>
<type value="数字"></type>
<meunit>个</meunit>
<number>20</number>
</iterm>
<iterm filed="">
<name>细菌阴阳性</name>
<code>zc</code>
<type value="单选框">
<option >阴</option>
<option>阳</option>
</type>
<required>false</required>
<normalValue>阴</normalValue>
</iterm>
<iterm filed="">
<name>照片</name>
<code>tp</code>
<type value="图片"></type>
<required>false</required>
</iterm>
<iterm filed="">
<name>描述</name>
<code>ms</code>
<type value="文本框"></type>
<required>false</required>
</iterm>
</custom>