[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 【已解决】求助批处理从XML中提取多处内容并输出到txt文档。

本帖最后由 licunwei 于 2024-5-27 14:49 编辑

xml文档内容如下:
<?xml version="1.0" encoding="UTF-8"?>

-<datas>


-<data>

<attribute value="S000351001007020100502024052608060748" name="id"/>

<attribute value="{"AUTOVC":"0","ActualFeeClass":"1","AutoAxisInfo":"","AutoVLP":"默A00000","AutoVLPC":"9","AxisInfo":"0","AxleNum":"0","BL_Center":"203","BL_SubCenter":"517","CPCExpireTime":"","CPCID":"","CPCIssueOrg":"成资渝乐至通旅","CPCPower":"0","CPCPowerState":"0","CPCStartTime":"","CPCVer":"0","CPUCardId":"1959234389545846","CPUCardSnNo":"1959234389545846","CPUCardType":"2","CPUEndDate":"20290904","CPUIssueID":"四川Q\u0005","CPUIssuer":"5105","CPUNetID":"5105","CPUStartDate":"20190904","CPUVehicleType":"1","CPUVersion":"67","CPUVlp":"川A58UE0","CPUVlpc":"0","CardBal":"0","CardCnt":"0","CardID":"","CardLocalFee":"8723","CardLocalPayfee":"9183","CardNet":"","CardSN":"0","CardTotalFee":"8723","CardTotalPayfee":"9183","CardType":"2","DriveAxisConfidience":"0","DriveAxisCount":"0","DriveMaxWeight":"252037885","DriveShaftIndex":"0","DriveShaftNum":"0","DriveVehAxisInfo":"0","DriveVehTranAxleType":"0","ETCtotalAmount":"0","EnAxleNum":"2","EnLane":"00","EnLaneType":"0","EnLimit":"0","EnOper":"","EnPSamTerm":"","EnSpTruck":"255","EnStation":"000","EnStdVehType":"0","EnTime":"2024-05-26T08:06:07","EnVC":"0","EnVLP":"川M6081C","EnVLPC":"0","EnVT":"0","EnWasteSN":"0","EnWeight":"0","GBLaneID":"S00035100100702010050","GBStationID":"S0003510010070","HandChioceAxis":"0","HandChioceAxisMenu":"0","InductCnt":"0","KeyVer":"0","LaneAppver":"2.3.6.5","LaneHex":"5101034536","LaneID":"54","LaneRole":"11100000","LaneState":"1","LimitWeight":"0","LoginTime":"2024-05-25T17:00:01","MonitorTime":"20240526080607","OBUEndDate":"20300322","OBUFeeSumAfter":"8723","OBUFeeSumBefore":"0","OBUInfoTypeRead":"-1","OBUInfoTypeWrite":"-1","OBULastGantryHex":"000000","OBULastGantryTime":"1970-01-01T08:00:00","OBUMAC":"0","OBUMileageAfter":"0","OBUMileageBefore":"0","OBUPassState":"2","OBUProvFeeSumAfter":"8723","OBUProvFeeSumBefore":"0","OBUProvPayFeeSumAfter":"0","OBUProvPayFeeSumBefore":"0","OBUProvTradeSuccNumAfter":"1","OBUProvTradeSuccNumBefore":"0","OBUSN":"5104884212038086","OBUStartDate":"20200322","OBUState":"97176469","OBUTotalTradeSuccNumAfter":"1","OBUTotalTradeSuccNumBefore":"0","OBUTradeResult":"0","OBUVehicleType":"1","OBUVersion":"67","OBUVlp":"川M6081C","OBUVlpc":"0","OBUdiscountFeeSumAfter":"0","OBUdiscountFeeSumBefore":"0","OBUpayFeeSumAfter":"0","OBUpayFeeSumBefore":"0","OBUtotalCount":"1","ObuIssueFlag":"四川Q\u0004","OccurTime":"2024-05-26T08:06:07","Oper":"000000999","OperName":"ETC操作员","OverWeightRate":"0","PCRSUVersion":"0","PSAMTradeSN":"00000000","PSamTerm":"215100005789","PVC":"0","PVT":"0","ParaVerPreNew":"8420240510009","PayCardBal":"0","PayCardID":"","PayCardNet":"","PayCardTranSN":"0","PreAxisInfo":"","PreAxleNum":"0","PreLimitWeight":"0","PreOverWeightRate":"0","PreTotalWeight":"0","QryEnStation":"000","RSUManuID":"337","ReleaseTime":"2024-05-26T08:06:07","Shift":"02","StationHex":"51010345","StationID":"345","StdVehType":"0","TAC":"00000000","TotalWeight":"0","TransType":"09","TriggerTime":"2024-05-26T08:06:07","UnionPayInfo":"","VC":"0","VLP":"川M6081C","VLPC":"0","VSpeed":"0","VT":"0","Vcnt":"0","VerifyCode":"0","WasteSN":"141","algorithmIdentifier":"1","antennaID":"54","appointId":"","batchNum":"S0003510010070201005020240525170001000000999","bnErrCause":"B409","cardSn":"0","cardVersion":"0","cardfeeSumAfter":"0","cardfeeSumBefore":"0","chargeMode":"1","checkSign":"0","collectFee":"0","consumeTime":"484","description":"0","direction":"2","discountFee":"0","discountFeeGroup":"0","discountType":"","enLaneHex":"5101034536","enStationHex":"51010345","enStdStation":"345","enTollLaneId":"S00035100100702010050","enTollStationId":"S0003510010070","enTollStationName":"成资渝乐至通旅","exHeight":"0","exLength":"0","exWidth":"0","fee":"0","feeBoardPlay":"1","feeCalcResult":"0","feeCalcSpecials":"0","feeGroup":"0","feeInfo1":"G:S000351001002220010|T:2024-05-26T07:57:36|ES:51011027|ET:2024-05-26T06:48:05|LG:5c6309|LT:2024-05-26T07:52:48|CN:5105|UT:0|AC:0|EAC:0|TT:1|CT:23|PH:b4a841353855453000000000|PC:0|VT:1|EVT:0|VC:0|FSL:-295276085|WL:0|TW:0|VSF:255|BH:|PSL:9183|RSL:8723|SP1:0|MV:20240327002|PV:20240510009|vc:0|vt:1|ac:0|ad:0|R:0,201,0=,0:0 ECODE:0x20000","feeInfo2":"8192","feeInfo3":"5D6316","feeMileage":"0","feeProvBeginHex":"","feeProvInfo":"","feeRate":"0.00","feeSpare1":"0","feeSpare2":"0","feeSpare3":"0","feeSumLocalAfter":"0","feeSumLocalBefore":"0","feeVehicleType":"1","gantryHex":"5D6316","gantryHexOpposite":"5C6316","gantryId":"S000351001002220010","gantryOrderNum":"201","gantryPassCount":"0","gantryPassInfo":"","gantryType":"1","holidayState":"0","hourBatchNo":"S00035100100702010020240525170001000000999","id":"S000351001007020100502024052608060748","identification":"2","interruptSignal":"1","intoTime":"20240526080607.110","invoiceCode":"","invoiceType":"0","lDate":"2024-05-25T00:00:00","laneFLag":"2","laneSign":"2","laneType":"1","lastGantryHex":"000000","lastGantryTime":"2024-05-26T08:06:00","logicalFileCause":"99","mediaNo":"5104884212038086","mediaType":"1","miliSecond":"172","modifyFlag":"1","noCardCount":"0","noCardTimesAfter":"0","noCardTimesBefore":"0","obuDiscountFee":"0","obuId":"5104884212038086","obuPayFee":"0","obuSign":"2","obuTotalAmount":"0","obuTotaldisCountAmount":"0","onLineFee":"0","onLinePayFee":"0","operationMedia":"1","originFee":"0","outofTime":"20240526080607.282","overHeight":"0","overLength":"0","overWidth":"0","paraVer":"2100020240419510002403270143000240113015700020240523450000000000015000240525016500050526016160002404150117000202110214200024041701880002007270387000200727036600024052633670002405263386000240526337000024052633830002202110180000240418017700024041801890002405240101000240425010800014090901090001912070114000210109011900020102601180002010260223000202404192500022021701240002024042612000140909018400000000000980002404170134000230923018420240510009","passId":"015105195923438954584600000000000000","passSign":"2","passState":"1","pathFitFlag":"0","payCardType":"","payFee":"0","payFeeGroup":"0","payOrderNum":"","postBalance":"0","preBalance":"0","provFee":"8723","provMinFee":"0","provTransCount":"1","provinceCount":"1","provinceDiscountFee":"0","provinceNumAfter":"2","provinceNumBefore":"0","provinceTransGroup":"51:0","rateCompute":"2","rateFitCount":"0","readCiphertextVerify":"-1","remarks":"aa2900510110273466526aa501035c6309665279d0b4a8413538554530000000000002ffffffffcbc4b4a85105000117435105195923438954584600010023df002213001101c35d000000000023df11002213cf69bd1d62ad5ea851002213","roadType":"1","serviceType":"1","shortFee":"0","shortFeeMileage":"0","signStatus":"1","spare1":"0","spcRateVersion":"24052401","specialType":"18","stationName":"成资渝乐至通旅","supplierID":"0","tac":"00000000","ticketFee":"0","tollIntervalId":"S000351001002220","tollIntervalSign":"1","tollLane":"54","tollLaneId":"54","tollModeVer":"5120240327002","tollParaVer":"5120240510009","tollProvinceId":"510201","tollStation":"000345","tollStationId":"345","tradeReadCiphertext":"0000000000000000","tradeResult":"0","tradeType":"0","tradeWriteCiphertext":"00000000","transCode":"0603","transFee":"8723","transFlag":"0","transPayType":"1","transTime":"2024-05-26T08:06:07","unifiedFee":"0","unpayFlag":"0","vehSpeed":"0","vehicleHight":"0","vehicleLength":"0","vehicleSeat":"0","vehicleSign":"0xff","vehicleUserType":"0","vehicleWidth":"0","wGroupID":"0","wLaneType":"4","wasteSpare1":"","wasteSpare4":"0","wasteSpare5":"0"}" name="msg"/>

<attribute value="0603" name="transCode"/>

</data>

</datas>
希望通过批处理脚本提取以下内容输出到txt文本。
CPCIssueOrg":"成资渝乐至通旅"        输出为           成资渝乐至通旅
EnTime":"2024-05-26T08:06:07"    输出为    时间:2024-05-26T08:06:07
CPUVlp":"川A58UE0"                      输出为    IC卡车牌:川A58UE0
EnVLP":"川M6081C“                       输出为    入口车牌:川M6081C
OBUVlp":"川M6081C"                     输出为    OBU车牌:川M6081C
VLP":"川M6081C”                          输出为     出口车牌:川M6081C
脚本不与xml放同一文件夹内,谢谢大家帮忙。

链接: https://pan.baidu.com/s/1gohDQY4m5p9jne6y6Ve3Zw 提取码: gnxp 复制这段内容后打开百度网盘手机App,操作更方便哦

回复 1# licunwei


    请把xml文件上传到网盘,以便测试代码。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher


以上传附件链接。

TOP

借助ai写了一条powershell换行命令。
  1. @echo off
  2. rem "批处理保存为utf-8编码格式"
  3. chcp 65001 >nul
  4. cd /d "%~dp0"
  5. set "file=D:\20240526080607480603.xml"
  6. set "_"CPCIssueOrg"=    "
  7. set "_"EnTime"=时间:"
  8. set "_"CPUVlp"=IC卡车牌:"
  9. set "_"EnVLP"=入口车牌:"
  10. set "_"OBUVlp"=OBU车牌:"
  11. set "_"VLP"=出口车牌:"
  12. (for /f "delims=" %%a in ('powershell -Command "(Get-Content '%file%' -Encoding UTF8) -replace '"', [Environment]::NewLine" ^|findstr /rvb "; , :"') do (
  13. if defined _"%%a" (
  14. call set str=%%_"%%a"%%
  15. ) else if defined str (
  16. call echo=%%str%%%%a
  17. set str=
  18. )
  19. ))>"out.txt"
  20. pause
复制代码
bat小白,请多指教!谢谢!

TOP

4楼代码 -replace '"' 改成 -replace '&去掉quot;'
去掉&后的“去掉”二字
bat小白,请多指教!谢谢!

TOP

本帖最后由 aloha20200628 于 2024-5-27 14:42 编辑

回复 1# licunwei

假设一楼测试文件或源文件是utf-8编码,存于 d:\xml\test.xml,则以下代码须用utf-8编码存为test.bat运行,其输出结果文件是 d:\xml\test.txt
  1. @echo off &setlocal enabledelayedexpansion &chcp 65001>nul
  2. for %%c in ("CPCIssueOrg:车站名称", "EnTime:时间", "CPUVlp:IC卡车牌", "EnVLP:入口车牌", "OBUVlp:OBU车牌", "VLP:出口车牌") do for /f "tokens=1-2 delims=:" %%1 in ("%%~c") do (set "%%~1=%%~2")
  3. (for /f "usebackq skip=9 delims=" %%a in ("d:\xml\test.xml") do (
  4. set "s=%%a"&for %%k in ("CPCIssueOrg", "EnTime", "CPUVlp", "EnVLP", "OBUVlp", "VLP") do (
  5. set "v=!s:*%%k=!"&for /f tokens^=^1^delims^=^" %%b in ("!v:~2,25!") do echo,!%%~k!:%%b
  6. )
  7. chcp 936>nul&endlocal&exit/b
  8. ))>"d:\xml\test.txt"
复制代码
假设一楼测试文件或源文件是ansi编码,存于 d:\xml\test.xml,则以下代码须用ansi编码存为test.bat运行,其输出结果文件是 d:\xml\test.txt
  1. @echo off &setlocal enabledelayedexpansion
  2. for %%c in ("CPCIssueOrg:车站名称", "EnTime:时间", "CPUVlp:IC卡车牌", "EnVLP:入口车牌", "OBUVlp:OBU车牌", "VLP:出口车牌") do for /f "tokens=1-2 delims=:" %%1 in ("%%~c") do (set "%%~1=%%~2")
  3. (for /f "usebackq skip=9 delims=" %%a in ("d:\xml\test.xml") do (
  4. set "s=%%a"&for %%k in ("CPCIssueOrg", "EnTime", "CPUVlp", "EnVLP", "OBUVlp", "VLP") do (
  5. set "v=!s:*%%k=!"&for /f tokens^=^1^delims^=^" %%b in ("!v:~2,25!") do echo,!%%~k!:%%b
  6. )
  7. endlocal&exit/b
  8. ))>"d:\xml\test.txt"
复制代码
1

评分人数

TOP

回复 6# aloha20200628

老师 帮忙再看看呢 匹配不上
C:\Users\Administrator\Desktop\新建文件夹>for %c in ("CPCIssueOrg:车站名称" "EnTime:时间" "CPUVlp:IC卡车牌" "EnVLP:入口车牌" "OBUVlp:OBU车牌" "VLP:出口车牌") do for /F "tokens=1-2 delims=:" %1 in ("%~c") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("CPCIssueOrg:车站名称") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "CPCIssueOrg=车站名称" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("EnTime:时间") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "EnTime=时间" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("CPUVlp:IC卡车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "CPUVlp=IC卡车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("EnVLP:入口车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "EnVLP=入口车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("OBUVlp:OBU车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "OBUVlp=OBU车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("VLP:出口车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "VLP=出口车牌" )

C:\Users\Administrator\Desktop\新建文件夹>(for /F "usebackq skip=9 delims=" %a in ("d:\test.xml") do (set "s=%a"  & for %k in ("CPCIssueOrg" "EnTime" "CPUVlp" "EnVLP" "OBUVlp" "VLP") do (set "v=!s:*%k=!"  & for /F tokens=1delims=" %b in ("!v:~2,25!") do echo,!%~k!:%b )
chcp 936 1>nul  & endlocal & exit/b) )

C:\Users\Administrator\Desktop\新建文件夹>pause
Press any key to continue . . .

TOP

回复 7# licunwei

从回报信息看,楼主修改了源文件d:\xml\test.xml的路径为d:\test.xml,但要确保其修改路径的真实性,以及输出结果文件的路径...
我用楼主一楼提供的源文件示例已经完全测试通过,只要按照源文件和脚本文件的编码一致性正确操作即可...

TOP

网盘下载的 xml ," 被转义,行字符数超过 cmd 的最大值

用 powershell 方便些
  1. $ht = @{
  2. "CPCIssueOrg" = "车      :"
  3. "EnTime"      = "时间    :"
  4. "CPUVlp"      = "IC卡车牌:"
  5. "EnVLP"       = "入口车牌:"
  6. "OBUVlp"      = " OBU车牌:"
  7. "VLP"         = "出口车牌:"
  8. }
  9. $filename = ".\20240526080607480603.xml"
  10. [xml]$xml = Get-Content -Path $filename -Encoding "UTF8"
  11. $xml.datas.data.attribute | ForEach-Object {
  12. if ( $_.name -eq "msg" ) { $string = $_.value }
  13. }
  14. $string = $string.Substring(1,$string.length-2)
  15. $string = $string -replace '(?<="),',"`n"
  16. $string = $string -replace '"(\w+)":"(.*)"',"`$1`t`$2"
  17. $as = $string -split "`n"
  18. $as | ForEach-Object {
  19. $a = $_ -split "`t"
  20. if ( $ht[$a[0]] ) {
  21. Write-Host ( "{0}   {1}" -f $ht[$a[0]] , $a[1] )
  22. }
  23. }
复制代码

TOP

回复 8# aloha20200628


    用源文件提取不到xml文件中的内容。但是把源文件内容拷贝到TXT文档,更改后缀为xml,编码为UTF-8 ,这样是可以提取到所有数据的。帮我分析分析是什么原因呢。

TOP

回复 9# newswan


    我试试呢

TOP

如果正确
最后一段改为
  1. $as | ForEach-Object {
  2. $a = $_ -split "`t"
  3. if ( $ht[$a[0]] ) {
  4. "{0}   {1}" -f $ht[$a[0]] , $a[1]
  5. }
  6. } | Out-File z.txt
复制代码

TOP

回复 12# newswan


    老师 我的xml文件名称不固定,怎么自动检测文件夹内所有xml文件名称,再进行提取。

TOP

回复 13# licunwei
  1. $ht = @{
  2. "CPCIssueOrg" = "车      :"
  3. "EnTime"      = "时间    :"
  4. "CPUVlp"      = "IC卡车牌:"
  5. "EnVLP"       = "入口车牌:"
  6. "OBUVlp"      = " OBU车牌:"
  7. "VLP"         = "出口车牌:"
  8. }
  9. function get-info() {
  10. param(
  11. [string]$filexml ,
  12. [string]$filetxt
  13. )
  14. [xml]$xml = Get-Content -Path $filexml -Encoding "UTF8"
  15. $xml.datas.data.attribute | ForEach-Object {
  16. if ( $_.name -eq "msg" ) { $string = $_.value }
  17. }
  18. $string = $string.Substring(1,$string.length-2)
  19. $string = $string -replace '(?<="),',"`n"
  20. $string = $string -replace '"(\w+)":"(.*)"',"`$1`t`$2"
  21. $as = $string -split "`n"
  22. $as | ForEach-Object {
  23. $a = $_ -split "`t"
  24. if ( $ht[$a[0]] ) {
  25. "{0}   {1}" -f $ht[$a[0]] , $a[1]
  26. }
  27. } | Out-File $filetxt
  28. }
  29. $Path = "C:\Users\admin\Desktop"
  30. Get-ChildItem -Path $Path -Filter "*.xml" | ForEach-Object {
  31. get-info $_.FullName ( $_.DirectoryName + "\" + $_.BaseName + ".txt")
  32. }
复制代码
1

评分人数

TOP

回复 14# newswan


    非常感谢

TOP

返回列表