Extracting text from XML file via batch file

此生再无相见时 提交于 2019-12-02 01:01:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!