[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 WHY 于 2020-9-17 16:26 编辑

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

评分人数

TOP

返回列表