通过批处理文件从XML文件中提取文本

时间:2022-11-22 02:09:12

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!

我必须通过批处理文件从XML文件中提取某些文本。我需要提取的部分之一是字符串标记( example1 ),另一个是数据标记之间( example2 )。有什么想法?提前致谢!

3 个解决方案

#1


3  

@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-

输出OP的输入文件 -

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


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


1  

Check the xpath.bat script:

检查xpath.bat脚本:

call xpath.bat "xml.xml" "//data"

#1


3  

@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-

输出OP的输入文件 -

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


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


1  

Check the xpath.bat script:

检查xpath.bat脚本:

call xpath.bat "xml.xml" "//data"