找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23660|回复: 3

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

[复制链接]
发表于 2024-3-27 19:45:44 | 显示全部楼层 |阅读模式
本帖最后由 zhengwei007 于 2024-3-27 23:38 编辑

我有若干个XML文件,内容如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../xsd/multisell.xsd">
  3.         <npcs>
  4.                 <npc>32615</npc> <!-- Ishuma (Maestro) -->
  5.         </npcs>
  6.         <item>
  7.                 <!-- Vesper Cutter -->
  8.                 <ingredient count="1" id="13457" />
  9.                 <!-- Sirra's Blade -->
  10.                 <ingredient count="1" id="8678" />
  11.                 <!-- Dualsword Craft Stamp -->
  12.                 <ingredient count="1" id="5126" />
  13.                 <!-- Adena -->
  14.                 <ingredient count="7168000" id="57" />
  15.                 <!-- Vesper Dual Sword -->
  16.                 <production count="1" id="52" />
  17.         </item>
  18.         <item>
  19.                 <!-- Stormbringer -->
  20.                 <ingredient count="1" id="72" />
  21.                 <!-- Caliburs -->
  22.                 <ingredient count="1" id="75" />
  23.                 <!-- Dualsword Craft Stamp -->
  24.                 <ingredient count="1" id="5126" />
  25.                 <!-- Crystal (C-Grade) -->
  26.                 <ingredient count="183" id="1459" />
  27.                 <!-- Adena -->
  28.                 <ingredient count="548100" id="57" />
  29.                 <!-- Stormbringer*Caliburs -->
  30.                 <production count="1" id="2566" />
  31.         </item>
复制代码
希望通过批处理对以上所有文件内容输出到一个CSV文件,标题我写,格式如下:

  1. ingredientID        count        ingredientID        count        ingredientID        count        ingredientID        count        ingredientID        count        productionID        count
  2. 13457        1        8678        1        5126        1        57        7168000                        52        1
  3. 72        1        75        1        5126        1        1459        183        57        548100        2566        1
复制代码
以上就是最终格式,谢谢!

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2024-3-27 22:27:41 | 显示全部楼层
本帖最后由 ppll2030 于 2024-3-27 22:30 编辑

只处理同级目录下的xml文件
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%f in ('dir /b /a-d "*.xml"') do (
  3.         for /f tokens^=1^-5^delims^=^<^=^" %%1 in ('findstr /ic:"item" /ic:"count=" "%%f"') do (
  4.                 if "%%2" == "item>" set "v="
  5.                 if "%%2" == "ingredient count" set "v=!v!, %%5, %%3"
  6.                 if "%%2" == "production count" echo, !v:~1!, %%5, %%3
  7.         )
  8. )>>res.csv
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

 楼主| 发表于 2024-3-27 23:37:53 | 显示全部楼层
只处理同级目录下的xml文件
ppll2030 发表于 2024-3-27 22:27


太厉害了,感谢感谢!
发表于 2024-3-28 11:35:15 | 显示全部楼层
回复 1# zhengwei007

第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )的实现方式如下:

  1. gawk -v"FS=\042" "BEGIN{print "ingredientID\tcount\tingredientID\tcount\tingredientID\tcount\tingredientID\tcount\tingredientID\tcount\tproductionID\tcount"}/^[\t ]*<item>$/,/^[\t ]*<\/item>$/{if(/^[\t ]*<ingredient count=.+id=.+>$/){A[++i]=$4;B[i]=$2}if(/^[\t ]*<production count=.+id=.+>$/){A[6]=$4;B[6]=$2;C=A[1]"\t"B[1];for(i=2;i<7;i++)C=C"\t"A[i]"\t"B[i];print C;delete A;delete B;i=0}}" *.xml>sour.csv
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 03:22 , Processed in 0.021183 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表