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!
Asked
Active
Viewed 1.0k times
3

user1599051
- 43
- 1
- 1
- 7
-
Duplicate : http://stackoverflow.com/questions/23196417/extracting-string-from-any-non-binary-file-irrespective-of-its-location-within-f/23198173#23198173 – SachaDee Jul 24 '14 at 20:31
-
@sachadee It's not possible with a batch file? – user1599051 Jul 24 '14 at 20:42
-
@sachadee If not, how would I have to modify your "StringBetween.au3" script to accommodate my situation? – user1599051 Jul 24 '14 at 20:53
-
Which strings do you want to extract from the above line? example1 & example2? – gbabu Jul 24 '14 at 21:21
-
You may consider using Windows Powershell for this as it natively supports an XML DOM. – Steve Guidi Jul 24 '14 at 22:01
3 Answers
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-
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

gbabu
- 1,088
- 11
- 15
-
Thanks a lot!I think the script seems to have worked since no errors or anything emerged. However, is the output file supposed to be created in the same directory as the batch script? Because I can't find it. – user1599051 Jul 24 '14 at 23:25
-
I have modified the script to store the output in output.txt now. If it is working can you mark this as answer? – gbabu Jul 24 '14 at 23:30
-
I ran the script and this is what was in the output file: `string string data string string stringstring string string string data string string stringstring string string string data string string stringstring string` – user1599051 Jul 24 '14 at 23:38
-
OK. I'm not with my computer now and can't test it now. I can get it fixed in probably 8 hours from now. But o don't see any issues with the current code. Please ensure to not to miss any characters or not to add any extra spaces in my code. – gbabu Jul 24 '14 at 23:52
-
This works absolutely fine with the above example file I used. Have you tried with the above example file? check this out [link](http://s24.postimg.org/5racwxdj9/Capture.png) – gbabu Jul 25 '14 at 10:51
-
I followed what you did exactly and [this](http://snag.gy/404JO.jpg) is what happened. – user1599051 Jul 25 '14 at 18:43
-
That's weird. I have modified the code to remove the echo off which will help us to see the each steps, to diagnose the issue. Could you please do exactly what I have done [here](http://s24.postimg.org/6hn8ibztx/New_Picture.jpg) and post the screenshot? – gbabu Jul 25 '14 at 19:45
-
[Picture #1](http://snag.gy/0Dk5x.jpg), [Picture #2](http://snag.gy/0UK4Q.jpg), [Picture #3](http://snag.gy/W3wYL.jpg) – user1599051 Jul 25 '14 at 21:27
-
Well, Got the issue. you never said there are white space before `
` and also one of the line has only `` in it. Give me the content of the xml file exactly (upload it somewhere or edit the question and update it with proper formatting.) and I will make the necessary changes. – gbabu Jul 25 '14 at 22:13 -
Download it [here](http://www.datafilehost.com/d/5597b084)... Uncheck "Use our download manager and get recommended downloads" to download normally. Could you comment when you've downloaded it, I'd like to delete the link soon enough. Thanks – user1599051 Jul 25 '14 at 22:32
-
Nope. Checking your file. It is not possible to retrieve the string stored between and as it is not a single line and has multiple breaks. I could try to get the text between
-
Aacini's answer works for the text between `
` and ` `. Sorry for not specifying, I'm a noob. Thanks for everything anyways. – user1599051 Jul 25 '14 at 22:55 -
No probs. Sorry I couldn't help to get your final answer, but I have modified the code to get the content between `
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

Aacini
- 65,180
- 12
- 72
- 108
-
Works perfectly at extracting what's between `
` tags, but doesn't extract what's between `` tags. – user1599051 Jul 25 '14 at 17:41 -