问题
I have to extract certain text from an XML file via a batch file. One of the parts I need to extract is between string tags (<string>example1</string>) and the other is between data tags (<data>example2</data>). Any ideas how? Thanks in advance!
回答1:
@echo OFF
del output.txt
for /f "delims=" %%i in ('findstr /i /c:"<string>" xml_file.xml') do call :job "%%i"
goto :eof
:job
set line=%1
set line=%line:/=%
set line=%line:<=+%
set line=%line:>=+%
set line=%line:*+string+=%
set line=%line:+=&rem.%
echo.%line%>>output.txt
:eof
Output with OP's input file-
D:\>draft.bat
D:\>type output.txt
000000000@gmail.com
default
Web form password
www.instagram.com (000000000@gmail.com)
www.instagram.com
Cheers, G
回答2:
Try this:
@echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in ('findstr /I /L "<string> <data>" theFile.xml') do (
set "line=%%a"
set "line=!line:*<string>=!"
set "line=!line:*<data>=!"
for /F "delims=<" %%b in ("!line!") do echo %%b
)) > result.txt
回答3:
Check the xpath.bat script:
call xpath.bat "xml.xml" "//data"
来源:https://stackoverflow.com/questions/24943001/extracting-text-from-xml-file-via-batch-file