|
|
@@ -659,35 +659,29 @@ public class OssFileController {
|
|
|
if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
|
|
|
Node currentNode = childNodes.item(k);
|
|
|
String nodeName = currentNode.getNodeName();
|
|
|
- // 【核心新增:去除节点名中的 `-` 字符】
|
|
|
- String cleanNodeName = nodeName.replaceAll("-", ""); // 关键步骤:替换所有 `-` 为空
|
|
|
+ String cleanNodeName = nodeName.replaceAll("-", "");
|
|
|
NodeList grandChildNodes = currentNode.getChildNodes();
|
|
|
|
|
|
- // 拼接逻辑(改用处理后的 cleanNodeName)
|
|
|
String currentKeyPrefix;
|
|
|
if (currentLevel == 1) {
|
|
|
- // 第1层:用处理后的节点名作为前缀
|
|
|
currentKeyPrefix = cleanNodeName;
|
|
|
} else if (currentLevel == 2) {
|
|
|
- // 第2层:判断是否与第1层前缀同名(基于处理后的名称)
|
|
|
if (cleanNodeName.equals(parentPrefix)) {
|
|
|
currentKeyPrefix = parentPrefix;
|
|
|
} else {
|
|
|
currentKeyPrefix = parentPrefix + cleanNodeName;
|
|
|
}
|
|
|
} else {
|
|
|
- // 第3层及以上:正常拼接处理后的节点名
|
|
|
currentKeyPrefix = parentPrefix + cleanNodeName;
|
|
|
}
|
|
|
|
|
|
- // 获取节点文本值
|
|
|
String textValue = getNodeTextValue(grandChildNodes);
|
|
|
- if (StringUtils.isNotBlank(textValue)) {
|
|
|
- // 生成不重复的最终key(基于处理后的前缀)
|
|
|
- String finalKey = generateUniqueKey(currentKeyPrefix, keyCounter);
|
|
|
- map.put(finalKey, textValue);
|
|
|
- } else {
|
|
|
- // 无文本值,递归时传递处理后的前缀
|
|
|
+ // 修复:空文本但有子元素时仍递归(避免漏解析深层节点)
|
|
|
+ if (StringUtils.isNotBlank(textValue) || hasChildElements(grandChildNodes)) {
|
|
|
+ if (StringUtils.isNotBlank(textValue)) {
|
|
|
+ String finalKey = generateUniqueKey(currentKeyPrefix, keyCounter);
|
|
|
+ map.put(finalKey, textValue);
|
|
|
+ }
|
|
|
if (hasChildElements(grandChildNodes)) {
|
|
|
Map<String,String> childMap = xmlDataDispose(grandChildNodes, document, currentKeyPrefix, currentLevel + 1, keyCounter);
|
|
|
map.putAll(childMap);
|
|
|
@@ -705,14 +699,14 @@ public class OssFileController {
|
|
|
StringBuilder text = new StringBuilder();
|
|
|
for (int i = 0; i < childNodes.getLength(); i++) {
|
|
|
Node child = childNodes.item(i);
|
|
|
- if (child.getNodeType() == Node.TEXT_NODE) {
|
|
|
- String trimText = StringUtils.trim(child.getNodeValue());
|
|
|
- if (StringUtils.isNotBlank(trimText)) {
|
|
|
- text.append(trimText);
|
|
|
- }
|
|
|
+ // 修复1:新增CDATA节点处理(数电票核心字段多为CDATA包裹)
|
|
|
+ if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) {
|
|
|
+ // 修复2:先拼接原始值,最后统一trim(避免中间有效换行/空格被过滤)
|
|
|
+ text.append(child.getNodeValue());
|
|
|
}
|
|
|
}
|
|
|
- return text.toString();
|
|
|
+ // 仅首尾trim,保留中间有效文本(如Remark里的换行转义后内容)
|
|
|
+ return StringUtils.trim(text.toString());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -731,9 +725,17 @@ public class OssFileController {
|
|
|
* 生成不重复key(同名节点加序号,避免覆盖)
|
|
|
*/
|
|
|
private String generateUniqueKey(String keyPrefix, Map<String, Integer> keyCounter) {
|
|
|
- int count = keyCounter.getOrDefault(keyPrefix, 0) + 1;
|
|
|
- keyCounter.put(keyPrefix, count);
|
|
|
- return count == 1 ? keyPrefix : keyPrefix + count;
|
|
|
+ // 修复:先获取当前计数(初始为0),再判断是否需要加序号
|
|
|
+ int count = keyCounter.getOrDefault(keyPrefix, 0);
|
|
|
+ if (count == 0) {
|
|
|
+ // 首次出现:直接返回前缀,计数置为1
|
|
|
+ keyCounter.put(keyPrefix, 1);
|
|
|
+ return keyPrefix;
|
|
|
+ } else {
|
|
|
+ // 重复出现:计数+1,返回前缀+序号(如SellerName2)
|
|
|
+ keyCounter.put(keyPrefix, count + 1);
|
|
|
+ return keyPrefix + count;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@ApiOperation("处理数电发票数据中的购买方名称信息")
|