本帖最后由 WHY 于 2020-9-17 16:26 编辑
保存为 E:\XML\Test.ps1
运行方法:脚本用右键单击,选择 "使用 PowerShell 运行"
或者,在 cmd 命令行下,输入 PowerShell -exec bypass "&'E:\XML\Test.ps1'" 回车运行 | $srcDir = 'E:\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();COPY |
共9列:文件名,id,charName,value,value,value,unicode,PUA,搜索结果g | $srcDir = 'E:\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); | | | | 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; } | | 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; | | } | | } | | } | | $g = $xml.SelectSingleNode('//ns:g[@ref="#' + $id + '"]', $mgrNS); | | $text = $g.innerText; | | if( $text -ne $null ){ $arr[8] = $text; } | | $fs.WriteLine('"' + ($arr -join '","') + '"' ); | | } | | if($i % 500 -eq 0 ) { $fs.Flush(); } | | } | | | | $fs.Flush(); | | $fs.Dispose(); | | echo 'Done'; | | [console]::ReadLine();COPY |
|