简体   繁体   中英

Getting tags value from xml file and saving results in csv file using batch script

I have xml file and need to extract the required tags values and output the result in csv file with tag name has header and below it's value.

so far what I have tried gets the xml tag and values but all the results gets displayed in 1 column instead like table format.

xml file content(partial file content) :

<RSP>
<HSI>
<SN>CZ9V      </SN>
<SP>Pro Gen8</SP>
<UUID>C9V</UUID>
</HSI>
<STP>3</STP>
<cUUID>343-11</cUUID>
<NICS>
<NIC>
<PORT>1</PORT>
<MACADDR>01:51:f2</MACADDR>
</NIC>
<NIC>
<PORT>1</PORT>
<MACADDR>00:17</MACADDR>
</NIC>
<NIC>
<PORT>2</PORT>
<MACADDR>77:28:02</MACADDR>
</NIC>
<NIC>
<PORT>3</PORT>
<MACADDR>00:04</MACADDR>
</NIC>
<NIC>
<PORT>4</PORT>
<MACADDR>00:06</MACADDR>
</NIC>
</NICS>
</RSP>

output am getting from output.csv file :

SN,SP,UUID,MACADDR,cUUID
CZ9V
Pro Gen8
C9V

01:51:f2 343-11

What I was looking to get is :

SN,SP,UUID,MACADDR,cUUID
CZ9V,Pro Gen8,C9V,01:51:f2,343-11

when we open the output.csv in spreadsheet all the above result should be easy to filter if needed using filter option.

Code so far am to get :

echo SN,SP,UUID,MACADDR,cUUID> %outFile%
set req_tags=SN SP UUID MACADDR cUUID
for %%a in (%inFile%) do (
for %%c in (%req_tags%) do (
set "found="
set search_tag=%%c
for /f "tokens=2 delims=><  " %%b in ( ' type "%%a" ^|findstr /i !search_tag! ' ) do set found=%%b
 if defined found >> "%outFile%" echo !found! 
)
)

need to get the tag vlaue under respective tag name in output.csv file.

I took a stab at it. You were on the right track. The following code looks like it's working for the sample data you posted and it will keep working for any additional item you add inside the HSI element.

The only thing you need to modify is the CSV header line, but you could elect to pass that as a parameter to the batch file or to hardcode the values inside the batch file itself, as they are now:

@echo off
setlocal enabledelayedexpansion
set inFile="C:\tests\xmltocsv.txt"
set outFile="C:\tests\output.csv"
if exist %outFile% del %outFile%
set csvline=
echo SN SP UUID > %outFile%
(for /F "delims=" %%a in ('findstr /I /L /V "^<HSI" %inFile%') do (
  if "%%a" EQU "</HSI>" (
    echo !csvline! & set csvline=
  ) ELSE (
   set line=%%a & set "line=!line:*>=!" & for /F "delims=<" %%b in ("!line!") do (IF "%%b" NEQ " " (IF "!csvline!" NEQ "" (set csvline=!csvline! %%b) ELSE (set csvline=%%b)))
  )
)) >> %outFile%

Given the requirements have changed a bit, I elected to post a new answer instead of editing the original one. Not sure if that's the way to do things, but hey, I try my best... :)

Here's a version that doesn't care about the XML hierarchy, it just extracts token values from a list of specified elements. However, note that as I explained earlier, the case where multiple children may belong to a common parent element (even though the XML hierarchy doesn't reflect it) is not handled here. But the output is on a single line, as request by the OP:

@echo off
setlocal enabledelayedexpansion
set req_tags=SN SP UUID MACADDR cUUID
set inFile="C:\tests\xmltocsv.txt"
set outFile="C:\tests\output.csv"
if exist %outFile% del %outFile%

rem set the header line
set hLine=
for %%a in (%req_tags%) do (
  set hLine=!hLine!,%%a
)
call :outputlinetocsv "%hLine%"

rem add values to file
set csvLine=
for %%a in (%req_tags%) do (
  set sTag=%%a
  for /F "tokens=2 delims=<>" %%f in ('findstr /I /L "^</!sTag!" %inFile%') do (
    set line=%%f
    set "line=!line:*>=!"
    set csvLine=!csvLine!,!line!
  )
)
call :outputlinetocsv "%csvLine%"
goto :eof

:outputlinetocsv
REM remove starting comma
set myLine=%~1
set "myLine=!myLine:*,=!"
echo !myLine! >> %outFile%
goto :eof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM