Home > Mobile >  Extracting xml tags using a batch file
Extracting xml tags using a batch file

Time:01-12

Using a batch file , I have to extract the values of two tags from a xml file and write them into a .txt file in the following format :

#- Automatic Package Update
----------

Myurl = https://someurl

Myrevision = 26498

----------

Here is the xml file :

<?xml version="1.0" encoding="UTF-8"?>
<info>
<entry
   path="."
   revision="26498"
   kind="dir">
<url>https://someurl/Ramin</url>
<relative-url>^/</relative-url>
<repository>
<root>https://someurl/Ramin</root>
</repository>
<wc-info>
<wcroot-abspath>C:/Users/ramin</wcroot-abspath>
<schedule>normal</schedule>
<depth>infinity</depth>
</wc-info>
<commit
   revision="26498">
<author>ramin</author>
<date>2022-05-03T15:58:37.325680Z</date>
</commit>
</entry>
</info>

what I have to do is to get the value of the tag "url" and write it in front of "Myurl = " and also get the value of the tag "revision" and write it in front of "Myrevision" and save the result in a .txt file.

Using the answer here : https://stackoverflow.com/a/24944510/20053469 I managed to do this only for "url". I would like to know how to do this also for revision which is under the branch of "entry".

CodePudding user response:

The important point first: you should not use Batch files to process .xml files because if the format of the .xml file changes in any way, the Batch file will stop working.

However if you understand this point and the required processing is not too complicated, you can solve your problem in a simple way via a Batch file:

@echo off
setlocal

set "entry=" & set "Myrevision="
for /F "tokens=1-3 delims=<=>/ " %%a in ('findstr "entry revision url" input.xml') do (
   if "%%a" == "entry" (
      set "entry=1"
   ) else if "%%a" == "revision" (
      if defined entry if not defined Myrevision set "Myrevision=%%~b"
   ) else if "%%a" == "url" (
      set "Myurl=%%b//%%c"
   )
)

(
   echo #- Automatic Package Update
   echo ----------
   echo/
   echo Myurl = %Myurl%
   echo/
   echo Myrevision = %Myrevision%
   echo/
   echo ----------
) > output.txt

The output of this program is exactly the same you requested...

  • Related