package net.northking.cctp.element.internal.utils;


import net.northking.cctp.element.core.exception.ExecuteException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

/**
 * XmlUtils
 *
 * @author fengxiang.zhang
 * @date 2022/4/13
 * @since 1.0
 */
public class XmlUtils {

    public static Document document(String xml) {
        try {
            return DocumentHelper.parseText(xml);
        } catch (Exception e) {
            throw new ExecuteException("XML解析失败", e);
        }
    }

    public static String path(Node parent, String xpath) {
        Node node = parent.selectSingleNode(xpath);
        if (node == null) {
            throw new ExecuteException("未找到匹配的节点:" + xpath);
        }
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            return node.getStringValue();
        }
        return node.getText();
    }
}
