将少数字符串转换为一个字符串

时间:2015-12-24 10:54:01

标签: xml batch-file text-extraction

我有许多XML文件,包含许多不同的事件。任何事件都有一些标签:

<v8e:Event>
    <v8e:Level>Information</v8e:Level>
    <v8e:Date>2015-12-22T16:18:17</v8e:Date>
    <v8e:ApplicationName>1CV8</v8e:ApplicationName>
</v8e:Event>
<v8e:Event>
    <v8e:Level>Information</v8e:Level>
    <v8e:Date>2015-12-23T16:18:17</v8e:Date>
    <v8e:ApplicationName>1CV28</v8e:ApplicationName>
</v8e:Event>

我希望将此限制转换为:

Information, 2015-12-22T16:18:17, 1CV8
Information, 2015-12-23T16:18:17, 1CV28

我知道如何删除XML标记,但我不知道如何将不同的字符串连接在一起。

@echo off setlocal EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" *.xml') do (
   set "line=%%a"
   set "line=!line:*<v8e:Level>=!"
   set "line=!line:*<v8e:Date>=!"
   set "line=!line:*<v8e:ApplicationName>=!"
   for /F "delims=<" %%b in ("!line!") do echo %%b
)) > 111.txt

正如我所说的那样:

Information
2015-12-22T16:18:17
1CV8
Information
2015-12-23T16:18:17
1CV28

我还想输出文件的名称与输入文件相同。 也许有人可以阻止我解决它?

1 个答案:

答案 0 :(得分:1)

你很近:

@echo off
setlocal EnableDelayedExpansion

set "output="
set "fields=0"
for /F "delims=" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" *.xml') do (
   set "line=%%a"
   set "line=!line:*<v8e:Level>=!"
   set "line=!line:*<v8e:Date>=!"
   set "line=!line:*<v8e:ApplicationName>=!"
   for /F "delims=<" %%b in ("!line!") do set "line=%%b"
   set "output=!output!!line!, "
   set /A fields+=1
   if !fields! equ 3 (
      set "file=%%a"
      for /F "delims=:" %%b in ("!file!") do echo !output:~0,-2!>> "%%~Nb.txt"
      set "output="
      set "fields=0"
   )
)

然而,这是我的方式:

@echo off
setlocal EnableDelayedExpansion

for %%f in (*.xml) do (

   set "output="
   set "fields=0"
   (for /F "tokens=3 delims=<>" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" "%%f"') do (
      set "output=!output!%%a, "
      set /A fields+=1
      if !fields! equ 3 (
         echo !output:~0,-2!
         set "output="
         set "fields=0"
      )
   )) > "%%~Nf.txt"

)