返回列表 发帖

[文本处理] 求助批处理从一堆XML中提取关键词整理到一个csv中

大家好,我有几百个xml文件,我的代码大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../xsd/skills.xsd">
<skill id="2606" levels="1" name="Gran Kain's Gift Box">
<table name="#extractableItems">
13259,1,0.8130;13258,1,1.626;13260,1,16.260;13261,10,4.645;13262,10,4.645;13263,10,4.645;13264,10,4.645;13265,10,4.645;13266,10,4.645;13267,10,4.645;13268,10,32.5260;13269,10,16.260
</table>
<!-- set name="reuseDelay" val="86400000" / -->
<set name="capsuled_items_skill" val="#extractableItems" />
<set name="isMagic" val="2" /> <!-- Static Skill -->
<set name="magicLevel" val="1" />
<set name="operateType" val="A1" />
<set name="targetType" val="SELF" />
<cond msgId="129">
<and>
<player invSize="10" />
<player weight="80" />
</and>
</cond>
<for>
<effect name="RestorationRandom" />
</for>
</skill>
<skill id="22109" levels="1" name="Shiny Halloween Toy Box">
<table name="#extractableItems">
20718,1,20;20719,1,20;20720,1,20;20721,1,20;20722,1,20
</table>
<set name="blockedInOlympiad" val="true" />
<set name="capsuled_items_skill" val="#extractableItems" />
<set name="itemConsumeCount" val="1" />
<set name="itemConsumeId" val="20717" />
<set name="isMagic" val="2" /> <!-- Static Skill -->
<set name="magicLevel" val="1" />
<set name="operateType" val="A1" />
<set name="targetType" val="SELF" />
<cond msgId="129">
<and>
<player invSize="10" />
<player weight="80" />
</and>
</cond>
<for>
<effect name="RestorationRandom" />
</for>
</skill>COPY
看着挺多,但我只需要<table name="#extractableItems">这个下面字段的值,并把字段值整理一下,最终效果是
2606 1 Gran Kain's Gift Box 13259 1 0.813
2606 1 Gran Kain's Gift Box 13258 1 1.626
2606 1 Gran Kain's Gift Box 13260 1 16.26
2606 1 Gran Kain's Gift Box 13261 10 4.645
2606 1 Gran Kain's Gift Box 13262 10 4.645
2606 1 Gran Kain's Gift Box 13263 10 4.645
2606 1 Gran Kain's Gift Box 13264 10 4.645
2606 1 Gran Kain's Gift Box 13265 10 4.645
2606 1 Gran Kain's Gift Box 13266 10 4.645
2606 1 Gran Kain's Gift Box 13267 10 4.645
2606 1 Gran Kain's Gift Box 13268 10 32.526
2606 1 Gran Kain's Gift Box 13269 10 16.26
22109 1 Shiny Halloween Toy Box 20718 1 20
22109 1 Shiny Halloween Toy Box 20719 1 20
22109 1 Shiny Halloween Toy Box 20720 1 20
22109 1 Shiny Halloween Toy Box 20721 1 20
22109 1 Shiny Halloween Toy Box 20722 1 20COPY

谢谢各位

还有一种,是这样的,也是几百个XML文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../xsd/items.xsd">
<item id="14849" type="EtcItem" name="Kahman's Supply Box">
<!-- Supply box received from Mercenary Kahman as mission compensation. Double click to check more information. -->
<set name="icon" val="icon.etc_jewel_box_i00" />
<set name="default_action" val="PEEL" />
<set name="immediate_effect" val="true" />
<set name="material" val="PAPER" />
<set name="weight" val="2" />
<set name="is_tradable" val="false" />
<set name="is_dropable" val="false" />
<set name="is_sellable" val="false" />
<set name="is_depositable" val="false" />
<set name="is_stackable" val="true" />
<set name="handler" val="ExtractableItems" />
<set name="capsuled_items" val="9625,1,1,100;9626,1,1,80" />
</item>
<item id="14850" type="EtcItem" name="Droph's Support Items">
<!-- Support items received from Dwarven Explorer Droph for defeating Hekaton Prime. Double click to check more information. -->
<set name="icon" val="icon.etc_jewel_box_i00" />
<set name="default_action" val="PEEL" />
<set name="immediate_effect" val="true" />
<set name="material" val="PAPER" />
<set name="weight" val="2" />
<set name="is_tradable" val="false" />
<set name="is_dropable" val="false" />
<set name="is_sellable" val="false" />
<set name="is_depositable" val="false" />
<set name="is_stackable" val="true" />
<set name="handler" val="ExtractableItems" />
<set name="capsuled_items" val="5575,140000,240000,100" />
</item>COPY
这个就是把带有capsuled_items的val值拿出来,也是整理成如下样式:
14849 EtcItem Kahman's Supply Box 9625 1 1 100
14849 EtcItem Kahman's Supply Box 9626 1 1 80
14850 EtcItem Droph's Support Items 5575 140000 240000 100COPY
再此谢谢各位大大。

TOP

回复 1# zhengwei007


   
rem 另存为 ANSI 编码 bat
' & cls & cscript.exe /nologo /e:vbscript "%~f0" %* > out.csv & pause & exit /b
Option Explicit
Dim oDOMDocument, oWshShell, oFSO, p, i
p = "."      '当前路径。可以指定其它路径,比如 d:\test\
Set oDOMDocument = CreateObject("Msxml2.DOMDocument")
Set oWshShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
p = oFSO.GetAbsolutePathName(p)
oWshShell.CurrentDirectory = p
For Each i In oFSO.GetFolder(p).Files
    If LCase(oFSO.GetExtensionName(i)) = LCase("xml") Then Call t(i.Path)
Next
Sub t(ByVal file)
    Dim s, i, x, oNode, oNodeList, m
    oDOMDocument.load file
    If oDOMDocument.parseError.errorCode <> 0 Then
        wsh.Echo file & " Error!"
        Exit Sub
    End If
    For Each oNode In oDOMDocument.documentElement.getElementsByTagName("skill")
        s = ""
        Set oNodeList = oNode.SelectNodes("@id")
        x = oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("@levels")
        x = x & vbTab & oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("@name")
        x = x & vbTab & oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("table[@name='#extractableItems']")
        For Each i In Split(oNodeList(0).text, ";")
            s = x
            For Each m In Split(i, ",")
                s = s & vbTab & m
            Next
            wsh.Echo s
        Next
    Next
End SubCOPY

QQ 20147578

TOP

回复 2# zhengwei007


   
rem 另存为 ANSI 编码 bat
' & cls & cscript.exe /nologo /e:vbscript "%~f0" %* > out.csv & pause & exit /b
Option Explicit
Dim oDOMDocument, oWshShell, oFSO, p, i
p = "."      '当前路径。可以指定其它路径,比如 d:\test\
Set oDOMDocument = CreateObject("Msxml2.DOMDocument")
Set oWshShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
p = oFSO.GetAbsolutePathName(p)
oWshShell.CurrentDirectory = p
For Each i In oFSO.GetFolder(p).Files
    If LCase(oFSO.GetExtensionName(i)) = LCase("xml") Then Call t(i.Path)
Next
Sub t(ByVal file)
    Dim s, i, x, oNode, oNodeList, m
    oDOMDocument.load file
    If oDOMDocument.parseError.errorCode <> 0 Then
        wsh.Echo file & " Error!"
        Exit Sub
    End If
    For Each oNode In oDOMDocument.documentElement.getElementsByTagName("item")
        s = ""
        Set oNodeList = oNode.SelectNodes("@id")
        x = oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("@type")
        x = x & vbTab & oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("@name")
        x = x & vbTab & oNodeList(0).value
        Set oNodeList = oNode.SelectNodes("set[@name='capsuled_items' and @val]")
        For Each i In Split(oNodeList(0).getAttribute("val"), ";")
            s = x
            For Each m In Split(i, ",")
                s = s & vbTab & m
            Next
            wsh.Echo s
        Next
    Next
End SubCOPY

QQ 20147578

TOP

返回列表