The line too long error only happens when FINDSTR reads the input via redirection or a pipe. That error goes away if you pass the name (path) of the file directly to FINDSTR.
But Aacini's code is dependent on the XML being formatted as you show in your original question. It will not work with unformatted XML, even if you fix the redirection issue.
If your input XML is truly unformatted, then there is a major risk that you cannot solve this with pure batch because batch is limited to reading lines with max length of ~8191 bytes.
I don't understand why you could not get it to work with powershell, given that it is designed to be able to work very well with XML.
Here is a solution that uses
JREPL.BATThe first JREPL extracts the last Proponix tag from the file, which is piped to the second JREPL that extracts all of the desired tags in "name=value" format. This output is read by FOR /F which then sets variables with the appropriate names.
Code: Select all
@echo off
setlocal
:: Define source file
set "file=test.xml"
:: Define tags of interest
set "final=Proponix"
set "tags=DateSent TimeSent MessageID InstrumentID"
:: Clear existing variables
for %%V in (%tags%) do set "%%V="
:: Extract the desired tags
for /f "delims=" %%A in (
'jrepl "<(%final%)>(?![\s\S]*<\1>)[\s\S]*</\1>" $0 /m /jmatch /f "%file%"^
^| jrepl "<(%tags: =|%)>\s*([\s\S]*?\S)\s*</\1>" "$1+'='+$2" /m /jmatch'
) do set "%%A"
:: Show the results
for %%V in (%tags%) do set %%V
--OUTPUT--
Code: Select all
DateSent=20151020
TimeSent=170633
MessageID=1077169400
InstrumentID=ID10001
The code should be fairly robust for most input, but there are some XML constructs that could trip it up.
Really you should return to powershell and process the XML properly.
Dave Benham