返回列表 发帖

【解决】40元求xml信息获取

本帖最后由 lxh623 于 2020-9-12 07:42 编辑

http://www.bathome.net/thread-56119-1-1.html
上次求了一次,觉得不够用。
第一,上一次的加上一个字段——篇名。(xml名字)有时候想找字。
第二,关于悉昙字和兰札字。
<char xml:id="SD-A442">
<charName>CBETA CHARACTER SD-A442</charName>
<charProp>
<localName>Romanized form in CBETA transcription</localName>
<value>ki</value>
</charProp>
<charProp>
<localName>Character in the Siddham font</localName>
<value></value>
</charProp>
<charProp>
<localName>Romanized form in Unicode transcription</localName>
<value>ki</value>
</charProp>
<mapping cb:dec="1066050" type="PUA">U+104442</mapping>
</char>
<char xml:id="RJ-CAC5">
<charName>CBETA CHARACTER RJ-CAC5</charName>
<charProp>
<localName>Romanized form in CBETA transcription</localName>
<value>hri</value>
</charProp>
<charProp>
<localName>rjchar</localName>
<value></value>
</charProp>
<charProp>
<localName>Romanized form in Unicode transcription</localName>
<value>hri</value>
</charProp>
<mapping cb:dec="1100485" type="PUA">U+10CAC5</mapping>
</char>COPY
第一个来自于T21n1320,第二个是T21n1419。
除了篇名,字符名,还有四个值。
还想要一个值,就是正文,得到字符名称后,搜索。比如,<g ref="#RJ-CB60">&#1100640;</g>或者<g ref="#SD-A44A">&#1066058;</g>。这一串或者中间的值,都可以。
结果当然需要UTF8。而且这个字符是乱码。仍然得到,主要是以后可以替换。

谢谢!

奇怪,浏览器乱码显示的数字大概就是最后一个字段括号内的数字,但是,替换的话,可能最好提取内容。

本帖最后由 lxh623 于 2020-9-12 07:25 编辑

谢谢两位。

TOP

本帖最后由 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();COPY
共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();COPY
1

评分人数

TOP

本帖最后由 zaqmlp 于 2020-9-11 22:18 编辑

回复 3# lxh623
<# :
cls
@echo off
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
pause
exit
#>
$path=$args[0];
$outfile=$path+'#result.csv';
$enc=[Text.Encoding]::UTF8;
$fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
$sw=New-Object System.IO.StreamWriter($fs, $enc);
$files=@(dir -liter $path -recurse|?{('.xml' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
for($i=0;$i -lt $files.length;$i++){
  write-host $files[$i].FullName;
  $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
  $m1=[regex]::matches($text, '<char xml:id="([^"]*?)">([\s\S]+?)</char>');
  if($m1.count -ge 1){
  foreach($k in $m1){
    $arr=@($files[$i].BaseName,'','','','','','','','','');
   
    $a=$k.groups[1].value;
    $arr[1]=$a;
    $b=[regex]::match($k.groups[2].value,'[^>]+(?=</charName>)');
    if($b.success){$arr[2]=$b.groups[0].value;};
   
    $m2=[regex]::matches($k.groups[2].value, '(?<=<value>).+?(?=</value>)');
    $n=3;
    if($m2.count -ge 1){
        foreach($v in $m2){
            $arr[$n]=$v.groups[0].value;
            $n++;
        };
    };
   
    $f=[regex]::match($k.groups[2].value,'<mapping type="[^"]*?unicode">([\s\S]+?)</mapping>');
    if($f.success){$arr[6]=$f.groups[1].value;};
   
    $g=[regex]::match($k.groups[2].value,'type="PUA">([\s\S]+?)</mapping>');
    if($g.success){$arr[7]=$g.groups[1].value;};
   
    $h=[regex]::match($k.groups[2].value,'cb:dec="([^"]+?)".+?type="PUA"');
    if($h.success){$arr[8]=$h.groups[1].value;};
   
    $reg='<g ref="#'+$a+'">(.+?)</g>'
    $p=[regex]::match($text, $reg);
    if($p.success){$arr[9]=$p.groups[1].value;};
   
    $line=$arr -join ',';
    $sw.WriteLine($line);
    $sw.Flush();
  };
  };
};
$sw.Close();
$fs.Close();COPY
1

评分人数

提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

本帖最后由 lxh623 于 2020-9-11 14:05 编辑

回复 2# zaqmlp

链接:https://pan.baidu.com/s/1YxgNeeQUNBCl0Iz5bNvkoQ
提取码:ix0w

与上次一样,得到excel。
篇名,字符名,三个<value>,最后一个PUA后面的值。
<char xml:id="SD-A442">得到字符名,正文有一段代码。

或者,上次的补加一些字段。

这次专门做一个搜索<g ref="#RJ-CB60">&#1100640;</g>,得到所有类似的代码。

谢谢!

TOP

完全看不懂要提取什么,发图示标明提取位置并把测试文件打包上传或发网盘
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

返回列表