windows 如何使用批处理脚本从xml文件中获取参数

eni9jsuy  于 2023-03-31  发布在  Windows
关注(0)|答案(2)|浏览(220)

Maven们
我有下面的test_123.xml文件。我需要提取几行,并将其移动到result.txt文件。似乎有一些问题,在我的代码。请帮助修复此代码提取所需的信息。

<?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>

我需要提取以下5个参数:

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

下面是从xml文件中提取5个参数的代码,但是我没有从下面的批处理脚本中得到想要的输出

@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文件未附加任何数据。
这是节点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" />

这是节点Id=“2”的预期输出

<Parameter Name="QueueManager" Value="QT0012"/>
x8diyxa7

x8diyxa71#

如果我们接受你想要的子字符串的唯一示例只出现一次,那么下面的批处理文件是否能帮助你?

@(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"

你甚至可以把它放在一行中:

@(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"

或者甚至从命令提示符窗口运行它:

(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"

根据您提交的XML文件内容,当前目录中result.txt的预期输出为:

<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"/>
iyr7buue

iyr7buue2#

试试这个:

@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

相关问题