本帖最后由 WHY 于 2020-9-17 16:26 编辑
保存为 E:\XML\Test.ps1
运行方法:脚本用右键单击,选择 "使用 PowerShell 运行"
或者,在 cmd 命令行下,输入 PowerShell -exec bypass "&'E:\XML\Test.ps1'" 回车运行- $srcDir = 'E:\xml'; #存放xml文件的目录路径
- $dstFile = 'E:\xml\Result.csv'; #输出文件路径
-
- $xml = New-Object System.XML.XmlDocument;
- $fs = New-Object System.IO.StreamWriter($dstFile, $false, [Text.Encoding]::UTF8);
- $files = dir -Literal $srcDir -Filter *.xml -Recurse | ?{$_ -is [IO.FileInfo]}
- $count = $files.Count;
-
- for($i=0; $i -lt $count; $i++){
- $xml.load($files[$i].FullName);
- $hash = @{};
- forEach( $node In $xml.GetElementsByTagName('g') ){
- $key = $node.ref;
- if( !$hash.ContainsKey($key) ){ $hash[$key] = $node.innerText; }
- }
- forEach( $node In $xml.GetElementsByTagName('char') ){
- $arr = @($files[$i].BaseName, '0', '0', '0', '0', '0', '0', '0', '0');
- $k = 3; $id = $node.id;
- if( $id -ne $null ) { $arr[1] = $id; }
- if( $node.charName -ne $null ) { $arr[2] = $node.charName; }
- $k = 3;
- forEach( $prop In $node.charProp ) {
- $value = $prop.value;
- if( $value -ne $null ) { $arr[$k++] = $value; }
- }
- forEach( $mapp In $node.mapping ) {
- $type = $mapp.type;
- if( $type -ne $null ) {
- if( $type.EndsWith('unicode') ){
- $arr[6] = $mapp.innerText;
- } elseif( $type -eq 'PUA' ){
- $arr[7] = $mapp.innerText;
- }
- }
- }
- if( $hash.ContainsKey('#' + $id) ){ $arr[8] = $hash['#' + $id]; }
- $fs.WriteLine('"' + ($arr -join '","') + '"' );
- }
- if($i % 500 -eq 0 ) { $fs.Flush(); }
- }
-
- $fs.Flush();
- $fs.Dispose();
- echo 'Done'
- [console]::ReadLine();
复制代码 共9列:文件名,id,charName,value,value,value,unicode,PUA,搜索结果g- $srcDir = 'E:\xml'; #存放xml文件的目录路径
- $dstFile = 'E:\xml\Result.csv'; #输出文件路径
-
- $xml = New-Object System.XML.XmlDocument;
- $fs = New-Object System.IO.StreamWriter($dstFile, $false, [Text.Encoding]::UTF8);
- $files = dir -Literal $srcDir -Filter *.xml -Recurse | ?{$_ -is [IO.FileInfo]}
- $count = $files.Count;
-
- for($i=0; $i -lt $count; $i++){
- $xml.load($files[$i].FullName);
- $mgrNS = New-Object System.XML.XmlNameSpaceManager($xml.NameTable);
- $mgrNS.AddNameSpace('ns', $xml.DocumentElement.NameSpaceURI); #xml命名空间
-
- forEach( $node In $xml.SelectNodes('//ns:char', $mgrNS) ){
- $arr = @($files[$i].BaseName, '0', '0', '0', '0', '0', '0', '0', '0');
- $id = $node.id;
- if( $id -ne $null ) { $arr[1] = $id; } #第2列:id
- if( $node.charName -ne $null ) { $arr[2] = $node.charName; } #第3列:charName
- $k = 3;
- forEach( $prop In $node.charProp) {
- $value = $prop.value;
- if( $value -ne $null ) { $arr[$k++] = $value; } #第4-6列:value
- }
- forEach( $mapp In $node.mapping ) {
- $type = $mapp.type;
- if( $type -ne $null ) {
- if( $type.EndsWith('unicode') ){
- $arr[6] = $mapp.innerText; #第7列:type='unicode'对应的文字
- } elseif( $type -eq 'PUA' ){
- $arr[7] = $mapp.innerText; #第8列:type='PUA'对应的文字
- }
- }
- }
- $g = $xml.SelectSingleNode('//ns:g[@ref="#' + $id + '"]', $mgrNS);
- $text = $g.innerText;
- if( $text -ne $null ){ $arr[8] = $text; } #第9列:节点g属性ref="#id"对应的文字
- $fs.WriteLine('"' + ($arr -join '","') + '"' );
- }
- if($i % 500 -eq 0 ) { $fs.Flush(); }
- }
-
- $fs.Flush();
- $fs.Dispose();
- echo 'Done';
- [console]::ReadLine();
复制代码
|