0

I have this batch script im working on, the point is to extract certain information from an XML and output the results in a text file. I am able to make it work however, I would like my results to come out in rows.

    @echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<XOrigin> <YOrigin> <XExtent> <YExtent>" grid_crawler_output.xml') do (
   set "line=%%a"
   set "line=!line:*<XOrigin>=!"
   set "line=!line:*<YOrigin>=!"
   set "line=!line:*<XExtent>=!"
   set "line=!line:*<YExtent>=!"
   for /F "delims=<" %%b in ("!line!") do echo %%b
)) > result.txt

My Result is

502800
928000
23650
30750

I would like for it to come out

502800,928000,23650,30750

Any help?

A snip of the source information

- <Projection>
  <Mapsheet /> 
  <Projection /> 
  <CentralMeridian>0</CentralMeridian> 
  <Spheroid /> 
  </Projection>
  <MaprecordProjection /> 
- <CoordinateInfo>
  <CoordinateType>Rectangular</CoordinateType> 
  <GridType>Unrotated</GridType> 
  <XOrigin>502800</XOrigin> 
  <YOrigin>928000</YOrigin> 
  <XExtent>23650</XExtent> 
  <YExtent>30750</YExtent> 

Another block within the XLM file

- <Projection>
  <Mapsheet /> 
  <Projection /> 
  <CentralMeridian>200</CentralMeridian> 
  <Spheroid /> 
  </Projection>
  <MaprecordProjection /> 
- <CoordinateInfo>
  <CoordinateType>Rectangular</CoordinateType> 
  <GridType>Unrotated</GridType> 
  <XOrigin>114438</XOrigin> 
  <YOrigin>863252</YOrigin> 
  <XExtent>534000</XExtent> 
  <YExtent>404000</YExtent> 
  </CoordinateInfo>
Alex
  • 3
  • 2

3 Answers3

0

what about putting all data into single variable and then echoing it to file?

@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<XOrigin> <YOrigin> <XExtent> <YExtent>" grid_crawler_output.xml') do (
   set "line=%%a"
   set "line=!line:*<XOrigin>=!"
   set "line=!line:*<YOrigin>=!"
   set "line=!line:*<XExtent>=!"
   set "line=!line:*<YExtent>=!"
   for /F "delims=<" %%b in ("!line!") do set "out=!out!%%b,"
))
echo %out% > result.txt
rostok
  • 2,057
  • 2
  • 18
  • 20
  • I ran it, it says spits out the text file with "ECHO is off". – Alex Sep 02 '14 at 13:44
  • *It spits out "Echo is OFF" – Alex Sep 02 '14 at 13:50
  • sorry, set was missing in second for – rostok Sep 02 '14 at 14:07
  • alternateively just change all the newlines into comma with repl.bat from http://www.dostips.com/forum/viewtopic.php?f=3&t=3855 `type export.txt | repl.bat "\r\n" "," MX` – rostok Sep 02 '14 at 14:23
  • Okay, it works well. I am missing one last step, would it be possible to separate every new block of data to a new row? See I have multiple blocks that have XOrigin and YOrigin. I will show the source of information. – Alex Sep 02 '14 at 15:48
0
@echo off
setlocal EnableDelayedExpansion
SET "build="
for /F "delims=" %%a in ('findstr /I /L "<XOrigin> <YOrigin> <XExtent> <YExtent>" q25624153.txt') do (
   set "line=%%a"
   set "line=!line:*<XOrigin>=!"
   set "line=!line:*<YOrigin>=!"
   set "line=!line:*<XExtent>=!"
   set "line=!line:*<YExtent>=!"
   for /F "delims=<" %%b in ("!line!") do SET "build=!build!,%%b"
   set "line=%%a"
   set "line=!line:*<YExtent>=!"
   IF "%%a" neq "!line!" ECHO(!build:~1!&SET "build="
)

GOTO :eof

I used a file named q25624153.txt containing your data for my testing and omitted the redirect-to-output-file.

You don't state whether this set of lines may be repeated in your file, but this routine will produce each set within each block found if there is more than one Xorigin..Yextent set. It assumes that the sequence of required data lines will be constant and complete.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Yea, I totally forgot to be explicit about that. However, your assumptions were correct, I did need it to scan each block and retrieve the data. Your suggestion was correct. Thanks! – Alex Sep 02 '14 at 16:06
0

Like the other posted answers, this solution is highly dependent on the format of the XML source. The format could easily be changed into an equivalent form that is still valid XML, yet it would break the solution. Ideally you should be using a tool built for parsing XML.

That being said...

Here is a pure batch solution that does minimal string manipulation. It relies on the fact that </CoordinateInfo> will follow each block of coordinates.

@echo off
setlocal enableDelayedExpansion
set "in=grid_crawler_output.xml"
set "out=result.txt"
set "ln="
(
  for /f "tokens=1,2 delims=<> " %%A in (
    'findstr /il "<XOrigin> <YOrigin> <XExtent> <YExtent> </CoordinateInfo>" "%in%"'
  ) do if "%%A"=="/CoordinateInfo" (
    echo(!ln:~0,-1!
    set "ln="
  ) else set "ln=!ln!%%B,"
)>"%out%"

The above can become quite slow if the input is large.

Below is another option that can process even very large input files quite efficiently. It relies on a hybrid JScript/batch utility called REPL.BAT that performs search/replace on stdin and writes the result to stdout. REPL.BAT is pure script that will work on any modern Windows machine from XP onward.

The solution can be written as a long one-liner. I chose to define some variables to help show the logic a bit better.

@echo off
setlocal
set "in=grid_crawler_output.xml"
set "out=result.txt"
set "search=<XOrigin> <YOrigin> <XExtent> <YExtent>"
set "val=[\s\S]*?>(.*?)<.*"
findstr /il "%search%" "%in%"|repl "%val%%val%%val%%val%" "$1,$2,$3,$4\r\n" mx >"%out%"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390