package com.jpsoft.ehome.xml;

import org.apache.commons.lang3.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import java.io.ByteArrayInputStream;

public class XmlParser {
    public static Element selectNode(String xml,String namespace, String path) throws Exception{
        SAXBuilder saxBuilder = new SAXBuilder();

//        String protocol = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
//
//        if (!xml.startsWith(protocol)){
//            xml = protocol + xml;
//        }

        Document document = saxBuilder.build(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        Element root = document.getRootElement();

        // 创建XPath对象
        XPath xpath = XPath.newInstance(path);

        if (StringUtils.isNotEmpty(namespace)) {
            xpath.addNamespace(namespace, "http://www.isapi.org/ver20/XMLSchema");
        }

        Element node = (Element )xpath.selectSingleNode(root);

        return node;
    }

    public static String selectNodeValue(String xml,String namespace, String path) throws Exception{
        Element node = selectNode(xml,namespace,path);

        if (node!=null) {
            return node.getText();
        }
        else{
            return null;
        }
    }

    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<PPVSPMessage>\n" +
                "<Version>4.0</Version>\n" +
                "<Sequence>51</Sequence>\n" +
                "<CommandType>REQUEST</CommandType>\n" +
                "<Command>ACS</Command>\n" +
                "<Params>\n" +
                "<DeviceID>test1</DeviceID>\n" +
                "<Time>2020-09-28 14:36:36</Time>\n" +
                "<MajorType>0x5</MajorType>\n" +
                "<MinorType>0x4c</MinorType>\n" +
                "<CardType>1</CardType>\n" +
                "<CardReaderNo>1</CardReaderNo>\n" +
                "<DoorNo>1</DoorNo>\n" +
                "<serialNo>1796</serialNo>\n" +
                "<currentVerifyMode>25</currentVerifyMode>\n" +
                "<currentEvent>ture</currentEvent>\n" +
                "<frontSerialNo>1795</frontSerialNo>\n" +
                "<thermometryUnit>celsius</thermometryUnit>\n" +
                "<currTemperature>36.6</currTemperature>\n" +
                "<isAbnomalTemperature>false</isAbnomalTemperature>\n" +
                "<RegionCoordinates>\n" +
                "<positionX>575</positionX>\n" +
                "<positionY>387</positionY>\n" +
                "</RegionCoordinates>\n" +
                "<mask>no</mask>\n" +
                "<PicDataUrl>http://223.75.76.159:6011/kms/services/rest/dataInfoService/downloadFile?id=62B21159627AA60B862F27595E20789C</PicDataUrl>\n" +
                "<thermalURL>http://223.75.76.159:6011/kms/services/rest/dataInfoService/downloadFile?id=35EF4158099BB12B5EC0CA83CE687C14</thermalURL>\n" +
                "</Params>\n" +
                "</PPVSPMessage>\n";

        try {
            String text = XmlParser.selectNodeValue(xml.trim(),null,"/PPVSPMessage/Params/Time");
            System.out.println(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
