[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]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,操作更方便哦

回复 22# licunwei

前面那一段,包含了顺序

TOP

回复 19# newswan

老师,我想知道你是添加的哪一条命令锁定的时间永远输出显示为第二行。

TOP

回复 20# licunwei
已经学会怎么添加提取项了。

TOP

回复 19# newswan


    老师 如果我要新增提取项,比如说增加了车道号 ,需要怎么添加命令呢,谢谢了。
        CPCIssueOrg = "车      :"
        lanID         ="车道号  :"
        EnTime      = "时间    :"
        CPUVlp      = "IC卡车牌:"
        EnVLP       = "入口车牌:"
        OBUVlp      = "OBU 车牌:"
        VLP         = "出口车牌:"

TOP

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

评分人数

TOP

回复 17# newswan
谢谢,源文件有2个数据。txt文档内时间能否显示到第二行呢,让车牌显示3-6行。

TOP

回复 16# licunwei


    你查看原始数据,是不是多了一个

TOP

回复 14# newswan

老师  提取出来的内容,输出第一个txt行数显示正确,第二个开始就会多一行,时间这行就会显示2次。第3个 4个等都会多出时间一行。

车      :   龙台站
IC卡车牌:   渝A02F6T
时间    :   2024-05-23T05:54:00
入口车牌:   渝A02F6T
OBU车牌:   渝A02F6T
出口车牌:   渝A02F6T
时间    :   2024-05-23T05:54:00

TOP

回复 14# newswan


    非常感谢

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

回复 12# newswan


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

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

回复 9# newswan


    我试试呢

TOP

回复 8# aloha20200628


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

TOP

返回列表