1. 解析xml的方式有很多种
1. DOM方法 2. SAX方法 3.JDOM方法 4.DOM4J方法
2. 例举DOM4J解析(解析比较复杂,含有样式解析,有删除样式和保存样式,过滤xml中删除的样式)
3. 引入依赖
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.4</version>
</dependency>
public Map<String, String> parseXml(byte[] data, String emrEditorType) {
Map<String, String> map = new HashMap<>(16);
try {
String blnr = new String(data, "UTF-8")
.replaceAll("[a-zA-Z0-9]+;", "").replace(' ', ' ');
blnr = blnr.replace(ConstantValues.HALF_SPACE, ConstantValues.SPACE);
Document doc = new SAXReader().read(new ByteArrayInputStream(blnr.getBytes("UTF-8")));
map = new LinkedHashMap<>();
HashSet set = new HashSet();
CommonUtils.parsingTrace(doc.getRootElement().elementIterator(), set, emrEditorType);
if (ConstantValues.CONSTANT_0.equals(emrEditorType)) {
CommonUtils.getTemplateMapByBackGroundTextWinning(doc.getRootElement().elementIterator(), map, set);
} else {
CommonUtils.getTemplateMapByBackGroundText(doc.getRootElement().elementIterator(), map, set);
}
} catch (Exception ex) {
if (!map.containsKey(ConstantValues.BODY_TEXT)) {
map.put(ConstantValues.BODY_TEXT, "");
}
logger.error("请求异常",ex);
return new HashMap<>();
}
return map;
}
public static void parsingTrace(Iterator it, HashSet set, String emrEditorType) {
while (it.hasNext()) {
org.dom4j.Element nextRoot = (org.dom4j.Element) it.next();
Iterator nextIt = nextRoot.elementIterator();
if (nextIt.hasNext()) {
parsingTrace(nextIt, set, emrEditorType);
} else {
String text = nextRoot.getName();
if (ConstantValues.CONSTANT_0.equals(emrEditorType)) {
if (ConstantValues.DELETER_INDEX.equals(text.trim()) && !ConstantValues.CONSTANT_NEGATIVE_1.equals(nextRoot.getText())) {
set.add(nextRoot.getParent().attribute("Index").getValue());
}
} else {
if (ConstantValues.DELETER_INDEX.equals(text.trim()) && !ConstantValues.CONSTANT_0.equals(nextRoot.getText())) {
set.add(nextRoot.getParent().attribute("Index").getValue());
}
}
}
}
}
public static void getTemplateMapByBackGroundText(Iterator it, Map<String, String> map, HashSet set) {
try {
while (it.hasNext()) {
org.dom4j.Element nextRoot = (org.dom4j.Element) it.next();
Iterator nextIt = nextRoot.elementIterator();
if (nextIt.hasNext()) {
getTemplateMapByBackGroundText(nextIt, map, set);
} else {
String text = nextRoot.getName();
if (ConstantValues.BACK_GROUND_TEXT.equals(text.trim())) {
StringBuilder elementValue = new StringBuilder();
parseTextAndValue(nextRoot, elementValue, map, set);
} else if (ConstantValues.CHECKED.equals(text.trim())) {
String s = nextRoot.getText();
if ("true".equals(s)) {
List nodes = nextRoot.getParent().getParent().elements("Element");
String key = ((Element) (((Element) nodes.get(0)).elements("Text")).get(0)).getText();
if (key.endsWith(":")) {
key = key.substring(0, key.indexOf(":"));
}
String value = ((Element) nextRoot.getParent().elements("Caption").get(0)).getText();
if (map.containsKey(key)) {
map.put(key, map.get(key) + value);
} else {
map.put(key, value);
}
}
}
}
}
} catch (Exception e) {
log.info(e);
}
}
public static void getTemplateMapByBackGroundText(Iterator it, Map<String, String> map, HashSet set) {
try {
while (it.hasNext()) {
org.dom4j.Element nextRoot = (org.dom4j.Element) it.next();
Iterator nextIt = nextRoot.elementIterator();
if (nextIt.hasNext()) {
getTemplateMapByBackGroundText(nextIt, map, set);
} else {
String text = nextRoot.getName();
StringBuilder elementValue = new StringBuilder();
Element root = nextRoot.getParent().element("XElements");
if (ConstantValues.BACK_GROUND_TEXT.equals(text.trim()) && root != null && root.element("Element") != null) {
parseTextAndValue(nextRoot, elementValue, map, set);
}
}
}
} catch (Exception e) {
log.info(e);
}
}
private static void parseTextAndValue(Element nextRoot, StringBuilder elementValue, Map<String, String> map, HashSet set) {
List nodes = nextRoot.getParent().element("XElements").elements("Element");
for (Iterator iterator = nodes.iterator(); iterator.hasNext(); ) {
Element elm = (Element) iterator.next();
if (elm.element("Text") != null && !set.contains(ObjectUtil.isEmpty(elm.attribute("StyleIndex")) ? "" : elm.attribute("StyleIndex").getValue())) {
elementValue = elementValue.append(elm.elementIterator().hasNext() ? elm.element("Text").getText() : "");
}
}
map.put(nextRoot.getText(), elementValue.toString().trim());
}