From ffb0fcb9072a043c333837fa83bd976b03fea091 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 02 十二月 2021 09:30:57 +0800
Subject: [PATCH] Merge pull request #259 from hotcoffie/wvp-28181-2.0
---
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 41 ++++++++++++++++++++++++++---------------
1 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index c82ba60..079a78b 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -1,15 +1,7 @@
package com.genersoft.iot.vmp.gb28181.utils;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
-
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
@@ -17,6 +9,13 @@
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
+
+import javax.sip.RequestEvent;
+import javax.sip.message.Request;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.util.*;
/**
* 鍩轰簬dom4j鐨勫伐鍏峰寘
@@ -114,12 +113,12 @@
// 濡傛灉鏄睘鎬�
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
- if (!isEmpty(attr.getValue())) {
+ if (!StringUtils.isEmpty(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
List<Element> chdEl = element.elements();
- if (chdEl.isEmpty() && !isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
+ if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
json.put(element.getName(), element.getText());
}
@@ -150,7 +149,7 @@
} else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
for (Object o : element.attributes()) {
Attribute attr = (Attribute) o;
- if (!isEmpty(attr.getValue())) {
+ if (!StringUtils.isEmpty(attr.getValue())) {
json.put("@" + attr.getName(), attr.getValue());
}
}
@@ -160,11 +159,23 @@
}
}
}
+ public static Element getRootElement(RequestEvent evt) throws DocumentException {
- public static boolean isEmpty(String str) {
- if (str == null || str.trim().isEmpty() || "null".equals(str)) {
- return true;
+ return getRootElement(evt, "gb2312");
+ }
+
+ public static Element getRootElement(RequestEvent evt, String charset) throws DocumentException {
+ Request request = evt.getRequest();
+ return getRootElement(request.getRawContent(), charset);
+ }
+
+ public static Element getRootElement(byte[] content, String charset) throws DocumentException {
+ if (charset == null) {
+ charset = "gb2312";
}
- return false;
+ SAXReader reader = new SAXReader();
+ reader.setEncoding(charset);
+ Document xml = reader.read(new ByteArrayInputStream(content));
+ return xml.getRootElement();
}
}
--
Gitblit v1.8.0