You are not logged in.

#1 12 Nov 2012 20:15

nkitson
Member
Registered: 12 Nov 2012
Posts: 3

Parsing parameters from an xml file

I need to extract 'Parameter ids' from an xml file

.
.
<Parameter id="qwdZjJLqbgeq0Mz9V90pHA==" subreportfilter="True">
  <Name>Mark date</Name> 
  <Type>Date</Type> 
  <PromptText>MarkBeforeDate</PromptText> 
<Values>
  <Date>2011-02-21T08:55:23</Date> 
  </Values>
  </Parameter>
<Parameter id="G4eLZ0AEAuyM7z6pd69+8Q==" subreportfilter="True">
  <Name>Mark date</Name> 
  <Type>Date</Type> 
  <PromptText>MarkAfterDate</PromptText> 
<Values>
  <Date>2010-08-30T09:21:30</Date> 
  </Values>
  </Parameter>
  </ReportParameters>
.
.

To make this futureproof, I would like to extract the Parameter ids where the <PromptText> is 'MarkBeforeDate' and 'MarkAfterDate' (ie qwdZjJLqbgeq0Mz9V90pHA==  and G4eLZ0AEAuyM7z6pd69+8Q== in the example data above)    Could this be done with a cmd script?

If impossible to reference the PromptText, as there are only two Paramter ids in the current file, which appear to be in a consistent order, I could extract the parameters as following
<Parameter id="
but I can't be certain whether the file will continue to only contain the two Parameters in the future.

Offline

#2 12 Nov 2012 20:26

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: Parsing parameters from an xml file

Not tested:

FOR /F "delims=^" tokens=1,2" %%A in ('type xmlfile.xml ^| find ^"Parameter id=^"') do (
     echo %%B
     echo %%B >> out.file 
)

But I would recommend you to use xml startlet:
http://xmlstar.sourceforge.net/

Offline

#3 13 Nov 2012 01:54

nkitson
Member
Registered: 12 Nov 2012
Posts: 3

Re: Parsing parameters from an xml file

'fraid this doesn't work - its not possible to use a " as a delims character, even if escaped.

this works (commented for benefit of anybody else coming across it) so long as the  ¿  character isn't in the Parameter ID.

@echo off
setlocal EnableDelayedExpansion
:: Read each line of file that contains "Parameter id=" into %%a
for /f "tokens=* usebackq" %%a in (`type test.xml ^| find "Parameter id="`) do (
    set z=%%a
    :: change quote charas into  ¿  (ok so long as Parameter id does not contain ¿
    set z=!z:"=¿!
    :: extract text between 1st and 2nd ¿ (originally " )
    for /f "tokens=1,2 delims=¿" %%a in ("!z!") do echo %%b
)

Thank you for

But I would recommend you to use xml startlet:
http://xmlstar.sourceforge.net/

which I will find very useful.  I don't think I will use it for this particular purpose though as the script will be deployed on customer servers and I would rather not deploy external utilities, even open source ones.

Would still appreciate an all script based solution that will work if additional Parameters are added to text.xml (the contents of this file are not under my control)

Offline

#4 13 Nov 2012 05:03

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: Parsing parameters from an xml file

I test this, based in npocmaka script:

FOR /F tokens^=2^ delims^=^" %%A in (
'type xmlfile.xml ^| find "Parameter id="'
) do (
     echo:%%A
)
pause

Last edited by carlos (13 Nov 2012 05:03)

Offline

#5 13 Nov 2012 05:46

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: Parsing parameters from an xml file

More improved:

FOR /F tokens^=2^ delims^=^" %%A in ('find "Parameter id=" xmlfile.xml') do echo:%%A

Last edited by carlos (13 Nov 2012 05:50)

Offline

#6 13 Nov 2012 17:57

nkitson
Member
Registered: 12 Nov 2012
Posts: 3

Re: Parsing parameters from an xml file

Many thanks Carlos.  The one line solutions are the best!

Nick

Offline

Board footer

Powered by