<?xml version="1.0"?> <Root> <rownum value="1"> <backupapplication>HP Data Protector</backupapplication> <policy>AUTDR12_Daily</policy> <policytype>FileSystem</policytype> <dataretained>31</dataretained> <fullbackup>7</fullbackup> <backuptime>0.17</backuptime> <retentionperiod>Short</retentionperiod> <peakmbps>11</peakmbps> <backupcategory>Fulls & Fulls</backupcategory> </rownum> <rownum value="2"> <backupapplication>HP Data Protector</backupapplication> <policy>AUTP_Appl_Monthly</policy> <policytype>FileSystem</policytype> <dataretained>268</dataretained> <fullbackup>91</fullbackup> <backuptime>2.31</backuptime> <retentionperiod>Long</retentionperiod> <peakmbps>12</peakmbps> <backupcategory>Fulls & Fulls</backupcategory> </rownum> </Root>
想把上面xml文件中的rownum值增加45,即1变成46、2变成47,但用正则表达式方式实现不了:
I tried the following code: NeedleRegEx = <rownum value="(\d+)"> Replacement = <rownum value="($1+45)"> Temp := RegExReplace(Temp,NeedleRegEx,Replacement) But this changes it into <rownum value="1+45"> while I want <rownum value="46">
AutoHotkey代码如下:
doc := ComObjCreate("MSXML2.DOMDocument.6.0") if(!doc.loadXml(xmlString)) { msgBox % "Hey! That's no valid XML!" ExitApp } rownums := doc.getElementsByTagName("rownum") Loop % rownums.length { rownum := rownums.item(A_Index-1) value := rownum.getAttribute("value") value += 45 rownum.setAttribute("value",value) } doc.save("myNewFile.xml")
完美解决!
原文链接:https://www.f2er.com/xml/293729.html