返回列表 发帖

[文本处理] 【已解决】求助批处理从一堆XML中提取所有关键词写到一个csv中

本帖最后由 zhengwei007 于 2025-2-7 10:55 编辑

我有N个XML碎文件,我希望把这些碎文件内容全部整理到一个csv中,方便我导入数据库。我挑几个例子展示,大概样式如下:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../xsd/npcs.xsd">
<npc id="20001" level="1" type="Monster">
<!-- Confirmed CT2.5 -->
<parameters>
<param name="MoveAroundSocial" value="0" />
<param name="MoveAroundSocial1" value="0" />
<param name="MoveAroundSocial2" value="0" />
</parameters>
<race>FAIRY</race>
<sex>MALE</sex>
<acquire exp="29" sp="2" />
<stats str="40" int="21" dex="30" wit="20" con="43" men="20">
<vitals hp="39.74519" hpRegen="2" mp="40.0" mpRegen="0.9" />
<attack physical="8.47458" magical="5.78704" random="30" critical="4" accuracy="4.75" attackSpeed="253" type="SWORD" range="40" distance="80" width="120" />
<defence physical="44.44444" magical="29.5916164000214" />
<attribute>
<defence fire="20" water="20" wind="53" earth="20" holy="20" dark="20" />
</attribute>
<speed>
<walk ground="20" />
<run ground="50" />
</speed>
<hitTime>370</hitTime>
</stats>
<status undying="false" canBeSown="true" />
<skillList>
<skill id="4408" level="1" />
<skill id="4409" level="1" />
<skill id="4410" level="11" />
<skill id="4411" level="11" />
<skill id="4412" level="11" />
<skill id="4413" level="11" />
<skill id="4414" level="2" />
<skill id="4415" level="3" />
<skill id="4416" level="13" />
</skillList>
<exCrtEffect>false</exCrtEffect>
<ai aggroRange="1000" clanHelpRange="300" isAggressive="false" />
<collision>
<radius normal="10" />
<height normal="15" />
</collision>
</npc>
<npc id="20003" level="5" type="Monster" name="Goblin">
<!-- Confirmed CT2.5 -->
<parameters>
<param name="MoveAroundSocial" value="42" />
<param name="MoveAroundSocial1" value="42" />
<param name="MoveAroundSocial2" value="42" />
</parameters>
<race>HUMANOID</race>
<sex>MALE</sex>
<equipment rhand="4" /> <!-- rhand: Club -->
<acquire exp="178" sp="5" />
<stats str="40" int="21" dex="30" wit="20" con="43" men="20">
<vitals hp="80.17319" hpRegen="2" mp="69.6" mpRegen="0.9" />
<attack physical="12.34006" magical="8.42666" random="50" critical="1" accuracy="9" attackSpeed="253" type="BLUNT" range="40" distance="80" width="120" />
<defence physical="51.60553" magical="34.3595464531323" />
<attribute>
<defence fire="20" water="20" wind="20" earth="20" holy="20" dark="20" />
</attribute>
<speed>
<walk ground="40" />
<run ground="110" />
</speed>
<hitTime>320</hitTime>
</stats>
<status undying="false" canBeSown="true" />
<skillList>
<skill id="4408" level="1" /> <!-- HP Increase (1x) -->
<skill id="4409" level="1" /> <!-- MP Increase (1x) -->
<skill id="4410" level="11" /> <!-- Average P. Atk. -->
<skill id="4411" level="11" /> <!-- Average M. Atk. -->
<skill id="4412" level="11" /> <!-- Average P. Def. -->
<skill id="4413" level="11" /> <!-- Average M. Def. -->
<skill id="4414" level="2" /> <!-- Standard Type -->
<skill id="4415" level="5" /> <!-- Blunt Weapons -->
<skill id="4416" level="6" /> <!-- Humanoids -->
</skillList>
<exCrtEffect>false</exCrtEffect>
<ai aggroRange="1000" clanHelpRange="300" isAggressive="false">
<clanList>
<clan>GOBLIN</clan>
</clanList>
</ai>
<dropLists>
<drop>
<item id="57" min="30" max="42" chance="70" /> <!-- Adena -->
<item id="112" min="1" max="1" chance="10.2689266243744" /> <!-- Apprentice's Earring -->
<item id="118" min="1" max="1" chance="7.65135117578888" /> <!-- Necklace of Magic -->
<item id="116" min="1" max="1" chance="15.60872152845" /> <!-- Magic Ring -->
<item id="1864" min="1" max="1" chance="1.95110591093254" /> <!-- Stem -->
<item id="1865" min="1" max="1" chance="0.975552955466271" /> <!-- Varnish -->
<item id="1866" min="1" max="1" chance="0.650366404044151" /> <!-- Suede -->
<item id="1873" min="1" max="1" chance="0.390218502666473" /> <!-- Silver Nugget -->
<item id="1868" min="1" max="1" chance="1.95110591093254" /> <!-- Thread -->
<item id="1788" min="1" max="1" chance="0.780450402933121" /> <!-- Recipe: Bow -->
<item id="8600" min="1" max="1" chance="23.1" /> <!-- Herb of Life -->
<item id="8601" min="1" max="1" chance="15.96" /> <!-- Greater Herb of Life -->
<item id="8602" min="1" max="1" chance="2.94" /> <!-- Superior Herb of Life -->
<item id="8603" min="1" max="1" chance="5.5" /> <!-- Herb of Mana -->
<item id="8604" min="1" max="1" chance="4.73" /> <!-- Greater Herb of Mana -->
<item id="8605" min="1" max="1" chance="0.77" /> <!-- Superior Herb of Mana -->
<item id="8606" min="1" max="1" chance="5" /> <!-- Herb of Power -->
<item id="8608" min="1" max="1" chance="5" /> <!-- Herb of Alacrity -->
<item id="8610" min="1" max="1" chance="5" /> <!-- Herb of Critical Attack - Probability -->
<item id="10655" min="1" max="1" chance="5" /> <!-- Herb of Vampiric Rage -->
<item id="10656" min="1" max="1" chance="5" /> <!-- Herb of Critical Attack - Power -->
<item id="8607" min="1" max="1" chance="5" /> <!-- Herb of Magic -->
<item id="8609" min="1" max="1" chance="5" /> <!-- Herb of Casting Spd. -->
<item id="8612" min="1" max="1" chance="0.33" /> <!-- Herb of the Warrior -->
<item id="8613" min="1" max="1" chance="0.33" /> <!-- Herb of the Mystic -->
<item id="8614" min="1" max="1" chance="0.34" /> <!-- Herb of Recovery -->
<item id="8611" min="1" max="1" chance="10.34" /> <!-- Herb of Speed -->
<item id="10657" min="1" max="1" chance="0.33" /> <!-- Herb of Doubt -->
<item id="13028" min="1" max="1" chance="0.33" /> <!-- Vitality Replenishing Herb -->
</drop>
<spoil>
<item id="1871" min="1" max="1" chance="5.6617" /> <!-- Charcoal -->
<item id="116" min="1" max="1" chance="45.2932" /> <!-- Magic Ring -->
<item id="1786" min="1" max="1" chance="4.5293" /> <!-- Recipe: Broad Sword -->
</spoil>
</dropLists>
<collision>
<radius normal="10" />
<height normal="16.5" />
</collision>
</npc>
</list>COPY
通过批处理输出结果如下:
npc id npc level npc type name parameters MoveAroundSocial parameters MoveAroundSocial1 parameters MoveAroundSocial2 race sex acquire exp acquire sp stats str stats int stats dex stats wit stats con stats men vitals hp vitals hpRegen vitals mp vitals mpRegen attack physical attack magical attack random attack critical attack accuracy attack attackSpeed attack type attack range attack distance attack width defence physical defence magical defence fire defence water defence wind defence earth defence holy defence dark walk ground run ground hitTime undying canBeSown skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level skillList id skillList level exCrtEffect aggroRange clanHelpRange isAggressive clan radius normal height normal
20001 1 Monster 0 0 0 FAIRY MALE 29 2 40 21 30 20 43 20 39.74519 2 40 0.9 8.47458 5.78704 30 4 4.75 253 SWORD 40 80 120 44.44444 29.5916164 20 20 53 20 20 20 20 50 370 FALSE TRUE 4408 1 4409 1 4410 11 4411 11 4412 11 4413 11 4414 2 4415 2 4416 3 4417 13 FALSE 1000 300 FALSE 10 15
20003 5 Monster Goblin 42 42 42 HUMANOID MALE 178 5 40 21 30 20 43 20 80.17319 2 69.6 0.9 12.34006 8.42666 50 1 9 253 BLUNT 40 80 120 51.60553 34.35954645 20 20 20 20 20 20 40 110 320 FALSE TRUE 4408 1 4409 1 4410 11 4411 11 4412 11 4413 11 4414 2 4415 5 4416 6 FALSE 1000 300 FALSE GOBLIN 10 16.5COPY
文本里面如equipment、dropLists这两段内容不要,没有字段值的请留空,执行完成后直接转成csv就行,谢谢各位。

本帖最后由 czjt1234 于 2025-2-1 15:49 编辑
rem 另存为 ANSI 编码 bat
' & cls & cscript.exe /nologo /e:vbscript "%~f0" %* > out.csv & pause & exit /b
Option Explicit
Dim oDOMDocument, oWshShell, oFSO, p, i, a, f, n
p = "."      '当前路径。可以指定其它路径,比如 d:\test\
f = vbTab    'csv文件的分隔符
n = 10       'skillList里面有10个skill。n必须≥2
a = Array("@id", _
          "@level", _
          "@type", _
          "@name", _
          "parameters/param[@name='MoveAroundSocial' and @value]", _
          "parameters/param[@name='MoveAroundSocial1' and @value]", _
          "parameters/param[@name='MoveAroundSocial2' and @value]", _
          "race", _
          "sex", _
          "acquire[@exp]", _
          "acquire[@sp]", _
          "stats[@str]", _
          "stats[@int]", _
          "stats[@dex]", _
          "stats[@wit]", _
          "stats[@con]", _
          "stats[@men]", _
          "stats/vitals[@hp]", _
          "stats/vitals[@hpRegen]", _
          "stats/vitals[@mp]", _
          "stats/vitals[@mpRegen]", _
          "stats/attack[@physical]", _
          "stats/attack[@magical]", _
          "stats/attack[@random]", _
          "stats/attack[@critical]", _
          "stats/attack[@accuracy]", _
          "stats/attack[@attackSpeed]", _
          "stats/attack[@type]", _
          "stats/attack[@range]", _
          "stats/attack[@distance]", _
          "stats/attack[@width]", _
          "stats/defence[@physical]", _
          "stats/defence[@magical]", _
          "stats/attribute/defence[@fire]", _
          "stats/attribute/defence[@water]", _
          "stats/attribute/defence[@wind]", _
          "stats/attribute/defence[@earth]", _
          "stats/attribute/defence[@holy]", _
          "stats/attribute/defence[@dark]", _
          "stats/speed/walk[@ground]", _
          "stats/speed/run[@ground]", _
          "stats/hitTime", _
          "status[@undying]", _
          "status[@canBeSown]", _
          "skillList/skill[@id and @level]", _
          "exCrtEffect", _
          "ai[@aggroRange]", _
          "ai[@clanHelpRange]", _
          "ai[@isAggressive]", _
          "ai/clanList/clan", _
          "collision/radius[@normal]", _
          "collision/height[@normal]")
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("npc")
        s = ""
        For Each i in a
            Set oNodeList = oNode.SelectNodes(i)
            x = ""
            If oNodeList.length = 1 Then
                If Left(i, 1) ="@" Then
                    x = oNodeList(0).value
                ElseIf UBound(Split(i, "@")) = 0 Then
                    x = oNodeList(0).text
                ElseIf UBound(Split(i, "@")) = 1 Then
                    x = RePlace(Split(i, "@")(1), "]", "")
                    x = oNodeList(0).getAttribute(x)
                ElseIf UBound(Split(i, "@")) = 2 Then
                    x = RePlace(Split(i, "@")(2), "]", "")
                    x = oNodeList(0).getAttribute(x)
                Else
                End If
            End If
            If oNodeList.length > 1 Then
                For Each m In oNodeList
                    x = x & m.getAttribute("id") & f & m.getAttribute("level") & f
                Next
                m = UBound(Split(x, f))
                x = x & String(n * 2 - m, f)
                x = Left(x, Len(x) - 1)
            End If
            s = s & x & f
        Next
        wsh.Echo s
    Next
End SubCOPY
楼主的输出结果把29.5916164000214输出为了29.5916164
这里采用批处理调用vbs,输出结果不会变

QQ 20147578

TOP

谢谢楼上,可以使用。

TOP

返回列表