2

i have been working with this idea as listed here

Parse XML file for attribute from batch file

But i cant comment to ask for more questions so i am asking for more help and this is not really a duplicate cause i changed several things

So my channelGuide.xml looks like this

<Guide>
   <listings>
      <channels>
         <channel ID='1' name='big bang' url='http://v.com/bigbang' quality='best' />
         <channel ID='2' name='AMerican Dad' url='http://v.com/americandad' quality='best' />
      </channels>
   </listings>
</Guide>

now my .bat file looks like this:

@echo off
cls
setlocal EnableDelayedExpansion

for /F tokens^=2^-9^,7delims^=^<^"^= %%a in (channelGuide.xml) do (
   if "%%a" equ "channel ID" echo(%%b %%c %%d %%e>>"%~d0%~p0\channelListings.txt"
)

but for some reason my .tx is coming back like this:

'1' name 'big bang' url 'http://v.com/bigbang' quality 'best' />
'2' name 'AMerican Dad' url 'http://v.com/americandad' quality 'best' />

I do not want the single quotes or anything but to look exactly like this:

1 big bang http://v.com/bigbang best
2 AMerican Dad http://v.com/americandad best
1

3 Answers 3

2
@ECHO OFF
SETLOCAL

for /F "tokens=2,4,6,8delims='" %%a in (q25617807.txt) do (
   if "%%d" neq "" echo(%%a %%b %%c %%d
)

GOTO :EOF

I used a file named q25617807.txt containing your data for my testing and removed the file redirection to suit my testing regime.


Explanation, using a single data line provided

   <channel ID='1' name='big bang' url='http://v.com/bigbang' quality='best' />

The data line is tokenised using the single-quote as a delimiter into tokens

  1. Spaces<channel ID=
  2. 1
  3. Spacename=
  4. big bang
  5. Spaceurl=
  6. http://v.com/bigbang
  7. Spacequality=
  8. best
  9. Space/>

Of these, tokens 2,4,6 and 8 are selected, so they get applied in sequence to %%a..%%d.

It's unlikely that another line would contain 4 strings in single-quotes (and not shown in original data) so simply filtering for "%%d" not being empty should be sufficient.

Time? After 13 years' unemployment, I've that in spades....

Sign up to request clarification or add additional context in comments.

5 Comments

if you have time maybe explain what things are doing here so i can learn more. and thank you for your help
might be problematic, if there are other lines with 8 tokens (or more) in the xml (which is not unlikely)
@Stephan : certainly. Would be easy to fix if required by changing the tokens assignment and analysing another token or tokens to gate the output. Not ss you say, unlikely - but possible. More likely that the program title may contain a single quote, but that would indicate a command of the language far beyond the apparent capabilities of the txt generation, sadly...
what if i was looking to just get info back for a certain id like id #2 ??
This is starting to become a saga. To extrat simply id=2, then modify the if statement to if "%%d" neq "" if "%%a" equ "2"... That is, when both the fourth field exists and the first is equal to 2.
1
for /f "tokens=1,2,3,5,7,9 delims='<" %%a in (channellistings.xml) do if "%%b"=="channel ID=" echo %%c %%d %%e %f

you should consider to delimit your output with (for example) ; instead of a space. Might be easier afterwards to process it. (for example reading it with excel)

Comments

1

you can try the xpath.bat - script that can get a value from xml file by given xpath:

for /f "tokens=* delims=" %%# in ('xpath.bat "chanel.xml" "//channel[0]/@name"') do (
  set name1=%%#
)

for /f "tokens=* delims=" %%# in ('xpath.bat "chanel.xml" "//channel[0]/@url"') do (
  set url1=%%#
)

for /f "tokens=* delims=" %%# in ('xpath.bat "chanel.xml" "//channel[0]/@quality"') do (
  set quality1=%%#
)

echo %name1% %url1% %quality1%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.