-2

Experts,

I have the test_123.xml file as below. I need to extract few of the lines and move it to result.txt file. There seem to be some issue in my code. Please help to fix this code to extract the required info.

<?xml version="1.0" encoding="ascii" ?>
<RunTimeModule Type="FlowManager">
  <Id>3</Id>
  <Name>test_123</Name>
  <StandAlone>Y</StandAlone>
  <Log>
    <Mode>DEBUG</Mode>
    <Path>/test01/log</Path>
    <Retention>7</Retention>
  </Log>
  <UXCodes>
    <System>UXtest510</System>
    <User>UXtest510</User> 
  </UXCodes>
  <Alert>
        <Frequency>15</Frequency>
  </Alert>
  <FlowManager>
        <LoopingFrequency>120</LoopingFrequency>
        <ProcessAndTerminate>N</ProcessAndTerminate>
        <CleanUpMemoryAfterProcess>Y</CleanUpMemoryAfterProcess>
        <CleanUpMemoryThreshholdInMB>5</CleanUpMemoryThreshholdInMB>
        <RetryAttempts>Infinite</RetryAttempts>
        <Node Id="1" Name="SourceFtp">
            <Adapter Type="Source" TypeName="Ftp" Module="ftp.dll"/>
            <InputParameters>
                <Parameter Name="FilePath" Value="/test_dev/TestSend/"/>
                <Parameter Name="FileName" Value="*.txt"/>
                <Parameter Name="MarkData">
                    <SubParameter Name="RenameFile" Value="Y"/>
                </Parameter>
                <Parameter Name="LogonAttempts" Value="10"/>
                <Parameter Name="FtpProperties">
                        <SubParameter Name="HostIP" Value="192.168.10.5"/>
                        <SubParameter Name="HostName" Value="test01"/>
                        <SubParameter Name="UserId" Value="test01"/>
                        <SubParameter Name="Password" Value="_SSC_PASSWORD_AT_LOGIN"/>
                        <SubParameter Name="Port" Value="21"/>
                        <SubParameter Name="TransferMode" Value="ASCII"/>
                        <SubParameter Name="CheckFileSize" Value="N"/>
                        <SubParameter Name="ConnectionTimeOut" Value="540"/>
                </Parameter>
            </InputParameters>
        </Node>
        <Node Id="2" Name="DestinationPriceQ">
            <Adapter Type="Destination" TypeName="MQ" Module="asmq.dll"/>
            <InputParameters>
                <Parameter Name="QueueManager" Value="QT0012"/>
                <Parameter Name="Queue" Value="EPH.TEST.IN.OUT"/>
                <Parameter Name="UseCCDT" Value="Y"/>
                <Parameter Name="PutMessageOptions">
                    <SubParameter Name="Group:EnableMessageGrouping" Value="N"/>
                </Parameter>
                                <Parameter Name="MQMD">
                                     <SubParameter Name="Segmentation" Value="N"/>
                                     <SubParameter Name="LogicalOrder" Value="N"/>
                                    <SubParameter Name="Format" Value="MQSTR"/>
                                </Parameter>
                <Parameter Name="$Data$" Map="Source.Ftp.1.$Data$"/>
            </InputParameters>
        </Node>
  </FlowManager>
</RunTimeModule>

I need to extract the below 5 parameters:

<HostIP> <UserId> <FilePath> <FileName> <QueueManager>

Here is the code written to extract the 5 parameters from the xml file. But i am not getting the desired output from the below Batch script

@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<HostIP> <UserId> <FilePath> <FileName> <QueueManager>" H:\test_123.xml') do (
   set "line=%%a"
   set "line=!line:*<HostIP>=!"
   set "line=!line:*<UserId>=!"
   set "line=!line:*<FilePath>=!"
   set "line=!line:*<FileName>=!"
   set "line=!line:*<QueueManager>=!"
   
   for /F "delims=<" %%b in ("!line!") do echo %%b
 )
) > result.txt

result.txt file is not appending any data.

This is the Expected output from Node Id="1".


<SubParameter Name="HostIP" Value="192.168.10.5" />
<SubParameter Name="UserId" Value="test01" />
<Parameter Name="FilePath" Value="/test_dev/TestSend"/>
<Parameter Name="FileName" Value="*.txt" />

This is the Expected output from Node Id="2"

<Parameter Name="QueueManager" Value="QT0012"/>
  • Please, take some time to read [how to ask](/help/how-to-ask) and [How to create a Minimal, Reproducible Example](/help/minimal-reproducible-example). There's no XML sample to work on; add a tag for the language used so it gets attention from those following it. Also, XML/HTML should be [parsed with proper tools](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). – LMC Mar 29 '23 at 15:32
  • There are clearly two ```Node Id``` and therefore two different `FileName`, `FilePath` strings, and your code is picking up only those in the first Id which exist, and the rest from the other. There is nothing in your question which therefore defines which it selects, from which Id, and how many possible Id's there could be. We therefore require more information. – Compo Mar 29 '23 at 17:04
  • Okay, so you decided to ignore my comment, and make an absolutely pointless modification of simply changing the parent directory name and xml file basename! Also when you decide to follow basic advice, in order for people to decide whether or not to assist you, please note that the XML file content you have posted is not valid. For that reason I would advise that you also try to post a version which does not include invalid XML, so that people could offer better solutions that do not use the same methodology as you decided to use. – Compo Mar 29 '23 at 17:44
  • @Compo Please dont think i am ignoring your comment. I also made some mistake which I am correcting so as to match the information provided. I am a beginer and not an expert like you. So it is going to take me a while to sort this out and I am still learning. Thanks much always for your feedback. Again, please dont think i am ignoring your comment. I am a slow learner. – Learning_something_new Mar 29 '23 at 17:47
  • @Compo I have updated the xml file. can you please help to rewrite the code to get the output. If possible, can you please provide me a sample batch script. – Learning_something_new Mar 29 '23 at 18:04
  • The XML file is now valid, thank you. However, as I have already asked, we need you to explain in your body text, how the programmer is supposed to determine which node to use for which Parameter/SubParameter, and how the actual file may differ from what you have submitted here. Is this determined by first encountered, Id number or the Name attributed to that Id? So do we always want the `HostIP`, `UserId`, `FilePath`, & `FileName` from Node `Id="1"`? or from Node `Name="SourceFtp"`? and do we always want the `QueueManager` from Node `Id="2"`? or from Node `Name="DestinationPriceQ"`? – Compo Mar 29 '23 at 18:26
  • thanks again@Compo. I now have updated the expected result and it tells from which Node the output is expected. Thanks much. – Learning_something_new Mar 29 '23 at 18:53
  • Simply looking at your code I see an obvious mistake, you are seemingly trying to replace everything on any line up to and including the first instance of substrings which do not exist! There is no line containing the following substrings, ``, ``, ``, ``, or ``. – Compo Mar 29 '23 at 19:34

2 Answers2

1

If we accept that the only instances of the substrings you want occur once, does the following batch file help you out?

@(For /F "Delims=>" %%G In ('%SystemRoot%\System32\findstr.exe
 /RI "\"FilePath\" \"FileName\" \"HostIP\" \"UserId\" \"QueueManager\""
 "H:\test_123.xml" 2^>NUL'
) Do @For /F "Tokens=*" %%H In ("%%G>") Do @Echo %%H) 1>"result.txt"

You could even have it in a single line:

@(For /F "Delims=>" %%G In ('%SystemRoot%\System32\findstr.exe /RI "\"FilePath\" \"FileName\" \"HostIP\" \"UserId\" \"QueueManager\"" "H:\test_123.xml" 2^>NUL') Do @For /F "Tokens=*" %%H In ("%%G>") Do @Echo %%H) 1>"result.txt"

Or even run it from a Command Prompt window:

(For /F "Delims=>" %G In ('%SystemRoot%\System32\findstr.exe /RI "\"FilePath\" \"FileName\" \"HostIP\" \"UserId\" \"QueueManager\"" "H:\test_123.xml" 2^>NUL') Do @For /F "Tokens=*" %H In ("%G>") Do @Echo %H) 1>"result.txt"

Based upon your submitted XML file content, the expected output in result.txt, in the current directory, would be:

<Parameter Name="FilePath" Value="/test_dev/TestSend/"/>
<Parameter Name="FileName" Value="*.txt"/>
<SubParameter Name="HostIP" Value="192.168.10.5"/>
<SubParameter Name="UserId" Value="test01"/>
<Parameter Name="QueueManager" Value="QT0012"/>
Compo
  • 36,585
  • 5
  • 27
  • 39
0

Try this:

@echo off
setlocal enabledelayedexpansion

set input_file=Test_123.xml
set output_file=result.txt

set node_id_1=false
set node_id_2=false

(for /f "tokens=*" %%a in ('type "%input_file%"') do (
  set line=%%a

  rem Check for Node Id="1"
  echo !line! | findstr /c:"<Node Id=\"1\"" >nul
  if !errorlevel! equ 0 set node_id_1=true

  rem Check for Node Id="2"
  echo !line! | findstr /c:"<Node Id=\"2\"" >nul
  if !errorlevel! equ 0 set node_id_2=true

  rem Extract lines for Node Id="1"
  if !node_id_1! (
    echo !line! | findstr /c:"<SubParameter Name=\"HostIP\"" >nul
    if !errorlevel! equ 0 echo !line! >> %output_file%

    echo !line! | findstr /c:"<SubParameter Name=\"UserId\"" >nul
    if !errorlevel! equ 0 echo !line! >> %output_file%

    echo !line! | findstr /c:"<Parameter Name=\"FilePath\"" >nul
    if !errorlevel! equ 0 echo !line! >> %output_file%

    echo !line! | findstr /c:"<Parameter Name=\"FileName\"" >nul
    if !errorlevel! equ 0 echo !line! >> %output_file%
  )

  rem Extract lines for Node Id="2"
  if !node_id_2! (
    echo !line! | findstr /c:"<Parameter Name=\"QueueManager\"" >nul
    if !errorlevel! equ 0 echo !line! >> %output_file%
  )

  rem Reset node flags after each node block
  echo !line! | findstr /c:"</Node>" >nul
  if !errorlevel! equ 0 set node_id_1=false & set node_id_2=false
)) >nul