package com.lantone.qc.dbanaly.facade.shaoyf;

import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.facade.comsis.XmlUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;
import java.util.Map;

/**
 * @Description: xml解析工具
 * @author: rengb
 * @time: 2020/3/28 14:23
 */
public class ShaoyfXmlUtil {

    /**
     * getText()获取当前节点的文本内容，getStringValue()获取所有子节点拼接后的文本内容
     *
     * @param xml
     * @return
     */

    public static Map<String, String> getXmlToMap(String xml) {
        Map<String, String> retMap = Maps.newLinkedHashMap();
        try {
            Document doc = DocumentHelper.parseText(XmlUtil.xmlErrorCorrection(xml));
            List<Element> emrTermElements = doc.getRootElement().element("TermList").elements("EMR-TERM");
            String ename, setnomark, text;
            for (Element emrTermElement : emrTermElements) {
                ename = emrTermElement.attributeValue("ename");
                if (StringUtil.isBlank(ename)) {
                    continue;
                }
                //                ename = StringUtil.removeBlank(ename);
                setnomark = emrTermElement.attributeValue("setnomark");
                setnomark = setnomark == null ? "" : setnomark;
                setnomark = setnomark.trim().replaceAll("&nbsp;", "");
                text = emrTermElement.getStringValue().trim().replaceAll("&nbsp;", "");
                if (StringUtil.isBlank(retMap.get(ename))) {
                    if (StringUtil.isBlank(text)) {
                        retMap.put(ename, setnomark);
                    } else {
                        retMap.put(ename, text + setnomark);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return XmlUtil.correctMapOfXml(retMap);
    }

}