[英]Extract a value from an xml file and output it to csv using a batch file
I need to extract the Value from a specific xml tag in all the xml files in a folder and export the value and file name w/o ext in 2 separate columns in a csv file. 我需要从文件夹中所有xml文件中的特定xml标记中提取值,然后在csv文件的2个单独的列中导出不带ext的值和文件名。 I have tried the follow with zero luck.
我尝试了零运气的跟随。
The xml looks like this: xml看起来像这样:
<ICSMXML xmlns="http://www.icsm.com/icsmxml" version="1.0">
<Header>
<MsgDelivery>
<To>
<Credential>
<Domain>ICSMID</Domain>
<Identity>11</Identity>
</Credential>
</To>
<From>
<Credential>
<Domain>DUNS</Domain>
<Identity>039576814</Identity>
</Credential>
</From>
<Sender>
<Credential>
<Domain>DUNS</Domain>
<Identity>039576814</Identity>
</Credential>
<UserAgent />
</Sender>
</MsgDelivery>
<MsgHeader>
<MessageId>10000095713</MessageId>
<Timestamp>04/12/2013 10:24:00 AM</Timestamp>
I need to parse the value from MessageId in any xml file found in the folder, and out put that in to an csv file along with the original file name w/o ext. 我需要从该文件夹中找到的任何xml文件中的MessageId中解析值,然后将其与原始文件名w / o ext一起放入一个csv文件中。 preferably have the value in column 1 and the file name wo ext in column 2.
最好在第1列中包含值,在第2列中包含文件名wo ext。
@echo off
call :check_lines < %1 > "%~N1.xml"
exit /b
REM Seek for the start of Data tag
:check_lines
set /P line=
if not "%line%" == "<MessageId>" goto check_lines
REM Copy until the end of Data tag
set /P line=
:put_lines
if "%line%" == "</MessageId>" goto end_lines
set /P line=%line%
goto put_lines
:end_lines
echo %line%
>>Message.csv
This should do it: 应该这样做:
@echo off
setlocal enabledelayedexpansion
for %%a in (*.xml) do (
call :XMLExtract "%%a" "<MessageID>" location
echo.!location!,%%~na
)
exit /b
:XMLExtract file keystart location
@echo off & setlocal
for /f "tokens=3 delims=<>" %%a in ('Findstr /i /c:%2 "%~1"') do (
set "loc=%%a" & goto :endloop
)
:endLoop
ENDLOCAL & IF "%~3" NEQ "" (SET %~3=%loc%) ELSE echo.%loc%
exit /b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.