Solved Read value Inside tags from xml using Batch Scrpit

August 27, 2012 at 03:32:44
Specs: Windows 7
Example : In test.xml, I have

<head>
<file name="input">
<above> <tag point="x"/> </above>
<below> <result first="00"/> </below>
</file>
<file name="output">
<above> <tag point="y"/> </above>
<below> <result first="11"/> </below>
</file>

</head>

I should read this XML in batch script where i should get value "11" .
This "11" is found inside tag <file name="output"> in tag <below> in <result first="11" >.

I have to get only the value "11" in the above specified path.

Thanks in advance


See More: Read value Inside tags from xml using Batch Scrpit

Report •

✔ Best Answer
August 28, 2012 at 23:17:20
this tries to do some pattern analysis:
::begin script
@echo off & setlocal enabledelayedexpansion
set f=
for /f "tokens=1,2 delims==_" %%a in (test.xml) do (
if /i "%%a"%%~b equ "<port name"write (
set f=1
) else (
if defined f (
if /i "%%a" equ "<return code" (
for /f "tokens=1" %%z in ("%%b") do echo TARGET:%%~z
set f=
)
)))
::end script


#1
August 27, 2012 at 03:35:54
I'm not quite with you. If you know the value is "11" why do you need to read it from the xml file? What exactly are you trying to do?

Report •

#2
August 27, 2012 at 03:45:56
@ijack
Please check the newly edited question above

Report •

#3
August 27, 2012 at 04:08:00
I'm afraid I think you are using the wrong tool for the job. Parsing XML files is best done with a language that has support for XML built in - C#, Perl, PHP would be examples.

Report •

Related Solutions

#4
August 27, 2012 at 04:44:30
Thanks @ijack ..
But do u know if der s any possible way to do by using batch script?
If it works by using other language,please let me know the solution

Report •

#5
August 27, 2012 at 05:01:33
Does <file name="output"> appear JUST ONCE?


====================================
Life is too important to be taken seriously.

M2


Report •

#6
August 27, 2012 at 18:47:50
short version:
for /f "tokens=2 delims==/" %%a in ('find /i "<below> <result first=" test.xml') do set out=%%a
:end one

longer version: (more dependable? i don'know)

@echo off & setlocal enabledelayedexpansion
for /f "usebackq tokens=1 delims=:" %%a in (`findstr /n /c:^"^<file name^=\^"output^" test.xml`) do set sk=%%a
set /a sk+=1
for /f "skip=%sk% tokens=2 delims==/" %%a in (fly2.xml) do set out=%%~a & goto :1
:1
echo %out%
:end two


Report •

#7
August 27, 2012 at 20:17:28
@Mechanix2Go <file name="output"> and <file name="input"> appear many times in an xml.I have posted one full part of it.
Here <file name="output"> ,"output" can also appear as "output_1" ,or "output_2a" etc.. similiarly for "input" as well

Report •

#8
August 27, 2012 at 20:29:12
@Thanks alot nBrane : It worked .. But please let me know the solution if there is more conditions like i have mentioned in my above comment...


Report •

#9
August 27, 2012 at 22:27:05
@nBrane
wat if the xml statement is like this,
<below>
<result first="00"/>
</below>
i mean to say, all the tags are in different lines.

Thanks in advance


Report •

#10
August 27, 2012 at 22:51:56
Yes, good! you are right. The script will fail if the "targets" are not set up correctly. If the target is too "loose", you get "false positives". If the target is too strict, as in my batch, you wil get "misses" that should be hits. My target was too "narrow", or specific. this is a quick fix, but not addressing the root of the problem:

for /f "tokens=2 delims==/" %%a in ('find /i "<result first=" test.xml') do set out=%%a

It is a matter of pattern application. I am unfamiliar with xml, and esp with this particular instance. The "front-line" user knows what he wants and what he has to work with. I, alas, am mostly ignorant and fumbling.


Report •

#11
August 28, 2012 at 01:41:01
Post the file if it's not too big.


====================================
Life is too important to be taken seriously.

M2


Report •

#12
August 28, 2012 at 02:32:02

<Main display="NORMAL">


<Port name="read_abc" exe="NO">
<input>
<struct file="C:\temp" sign="id1"/>
</input>
<output>
<return code="33" shortmsg="Implementation not found for commande."/>
</output>
</Port>
<Port name="write_abcd" exe="NO">
<input>
<struct file="C:\temp" id="id1"/>
</input>
<output>
<return code="1" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
</output>
</Port>


<Port name="read_qwe" exe="NO">
<input>
<struct file="C:\temp" sign="id2"/>
</input>
<output>
<return code="66" shortmsg="Implementation not found for commande."/>
</output>
</Port>
<Port name="write_qwer" exe="NO">
<input>
<struct file="C:\temp" id="id2"/>
</input>
<output>
<return code="0" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
</output>
</Port>

</Main>


I need to get the values for return code which are inside
<port name="write_* > ---> <output> --> and <return code=" x ">

here x varies ...
Here i have to get values "1" from <port name="write_abcd"> and
"0" from <port name="write_qwer">


Report •

#13
August 28, 2012 at 23:17:20
✔ Best Answer
this tries to do some pattern analysis:
::begin script
@echo off & setlocal enabledelayedexpansion
set f=
for /f "tokens=1,2 delims==_" %%a in (test.xml) do (
if /i "%%a"%%~b equ "<port name"write (
set f=1
) else (
if defined f (
if /i "%%a" equ "<return code" (
for /f "tokens=1" %%z in ("%%b") do echo TARGET:%%~z
set f=
)
)))
::end script

Report •

#14
August 29, 2012 at 03:59:41
@nbrane : Thanks its working but its not working for a proper xml...
i mean to say our code which is above looks like

<Port name="read_abc" exe="NO">
<input>
<struct file="C:\temp" sign="id1"/>
</input>
</port>
Your code works for this...

but
<Port name="read_abc" exe="NO">
_____<input>
__________<struct file="C:\temp" sign="id1"/>
_____</input>
</port>
Our XML is in this format and its not working for this.
Please consider space in place of "_" symbol


Report •

#15
August 29, 2012 at 04:13:33
Those two look identical to me. You need to wrap your formatted XML in
 tags so it maintains its formatting.

Tony


Report •

#16
August 29, 2012 at 04:47:35
@tonysathre : Pls consider the newly edited code

Report •

#17
August 29, 2012 at 04:59:53
I just tried nbrane's code and it worked for me using the below XML, formatted the way you specified above:

Output:

TARGET:1
TARGET:0

<Main display="NORMAL">

<Port name="read_abc" exe="NO">
     <input>
          <struct file="C:\temp" sign="id1"/>
</input>
<output>
<return code="33" shortmsg="Implementation not found for commande."/>
</output>
</Port>
<Port name="write_abcd" exe="NO">
     <input>
          <struct file="C:\temp" id="id1"/>
</input>
<output>
<return code="1" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
</output>
</Port>

<Port name="read_qwe" exe="NO">
     <input>
          <struct file="C:\temp" sign="id2"/>
</input>
<output>
<return code="66" shortmsg="Implementation not found for commande."/>
</output>
</Port>
<Port name="write_qwer" exe="NO">
     <input>
          <struct file="C:\temp" id="id2"/>
</input>
<output>
<return code="0" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
</output>
</Port>
</Main>

Tony


Report •

#18
August 29, 2012 at 05:12:50
@Tony : Yes, ur rite.
We cannot change the way xml is coz it is auto generated xml through our application. So we can only read it but cannot edit .

Report •

#19
August 29, 2012 at 05:28:06
Then post it exactly as it comes out, and wrap it in the < PRE ></ PRE > tags.

Tony


Report •

#20
August 29, 2012 at 20:59:31
@Tony

<Main display="NORMAL">
    <Port name="read_abc" exe="NO">
         <input>
                 <struct file="C:\temp" sign="id1"/>
         </input>
         <output>
                 <return code="33" shortmsg="Implementation not found for commande."/>
         </output>
   </Port>
   <Port name="write_abcd" exe="NO">
         <input>
                <struct file="C:\temp" id="id1"/>
         </input>
         <output>
               <return code="1" shortmsg="NOTEXECUTED" longmsg="Not execued"/>
         </output>
  </Port>
  <Port name="read_qwe" exe="NO">
        <input>
              <struct file="C:\temp" sign="id2"/>
        </input>
        <output>
               <return code="66" shortmsg="Implementation not found for commande."/>
        </output>
  </Port> 
  <Port name="write_qwer" exe="NO">
      <input>
               <struct file="C:\temp" id="id2"/>
      </input>
      <output>
               <return code="0" shortmsg="NOTEXECUTED" longmsg="Not execute"/>
      </output>
  </Port>
</Main>


Report •

#21
August 29, 2012 at 22:41:09
here is another attempt. i tried to "ignore" all leading spaces, but it still might not work. left a lot of debugging turds in the code.
@echo off & setlocal enabledelayedexpansion
set f=
for /f "tokens=1-5 delims==_ " %%a in (test.xml) do (
:debugging stuff
echo A:%%a[B:%%b[C:%%c[D:%d
pause > nul
if /i "%%a%%b"%%~c equ "<portname"write (
set f=1
echo FLAG SET
) else (
if defined f (
ECHO FLAG CHECK
if /i "%%a%%b" equ "<returncode" (
ECHO DELIVERY
for /f "tokens=1" %%z in ("%%c") do echo TARGET:%%~z
set f=
)
)))

Report •

Ask Question