You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.5 KiB
75 lines
2.5 KiB
1 month ago
|
import 'dart:io';
|
||
|
import 'package:xml/xml.dart';
|
||
|
|
||
|
void main2() {
|
||
|
const dictFilePath = "D:/PBHK/pbhk_trade/Sources/ServerUFT/UFT/metadata/dict.dict";
|
||
|
const fieldFilePath = "D:/PBHK/pbhk_trade/Sources/ServerUFT/UFT/metadata/stdfield.stdfield";
|
||
|
final dictFile = File(dictFilePath);
|
||
|
final fieldFile = File(fieldFilePath);
|
||
|
|
||
|
if (!dictFile.existsSync()) {
|
||
|
print('字典文件不存在: $dictFilePath');
|
||
|
return;
|
||
|
}
|
||
|
if (!fieldFile.existsSync()) {
|
||
|
print('字段文件不存在: $fieldFilePath');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// 1. 读取字典文件
|
||
|
final dictDocument = XmlDocument.parse(dictFile.readAsStringSync());
|
||
|
|
||
|
// 2. 读取字段文件
|
||
|
final fieldDocument = XmlDocument.parse(fieldFile.readAsStringSync());
|
||
|
|
||
|
// 3. 找出字典文件中所有 xsi:type="metadata:DictionaryType" 的items节点
|
||
|
final dictionaryItems =
|
||
|
dictDocument.findAllElements('items').where((element) {
|
||
|
return element.getAttribute('xsi:type') == 'metadata:DictionaryType';
|
||
|
}).toList();
|
||
|
|
||
|
// 4. 处理这些节点
|
||
|
for (final item in dictionaryItems) {
|
||
|
bool hasConstantName = false;
|
||
|
String? dictionaryName;
|
||
|
|
||
|
// 检查所有子节点
|
||
|
for (final child in item.childElements) {
|
||
|
final constantName = child.getAttribute('constantName');
|
||
|
if (constantName != null && constantName.isNotEmpty) {
|
||
|
hasConstantName = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 如果没有子节点包含constantName属性
|
||
|
if (!hasConstantName) {
|
||
|
dictionaryName = item.getAttribute('name');
|
||
|
|
||
|
if (dictionaryName != null && dictionaryName.isNotEmpty) {
|
||
|
// 在字段文件中查找是否有items节点的dictionaryType等于该name值
|
||
|
final matchingFieldItems = fieldDocument.findAllElements('items').where((element) {
|
||
|
return element.getAttribute('dictionaryType') == dictionaryName;
|
||
|
});
|
||
|
|
||
|
// 如果没有找到匹配的字段,则删除字典中的当前节点
|
||
|
if (matchingFieldItems.isEmpty) {
|
||
|
item.parent?.children.remove(item);
|
||
|
}
|
||
|
} else {
|
||
|
// 如果没有name属性,也删除该节点
|
||
|
item.parent?.children.remove(item);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 5. 保存修改后的字典文件
|
||
|
final outputPath = dictFilePath.replaceAll('.dict', '_modified.xml');
|
||
|
File(outputPath).writeAsStringSync(dictDocument.toXmlString(pretty: true));
|
||
|
print('处理完成,结果已保存到: $outputPath');
|
||
|
} catch (e) {
|
||
|
print('处理XML文件时出错: $e');
|
||
|
}
|
||
|
}
|