使用批处理解析XML文件来获得某些特定节点的值 [英] Parsing a XML file using batch to get values from some specific nodes

查看:2316
本文介绍了使用批处理解析XML文件来获得某些特定节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有列出每个节点列表作为它的父节点,我想获得/保存进前三节点的变量值,即进入输出标记
我知道该怎么做使用VBScript,但它更有趣的我有批量的解决方案。
拜托,能不能做到?

For each node list having Lists as its parent node, I want to get/save into a variable values of the first three node, namely entry, output and token. I know how to do that with vbscript, but it is much more interesting to me to have a solution in batch. Please, can it be done?


    

   <list1>
      <entry>myEntry</entry>
      <output>myOut</output>
      <token>4</token>
              <status>1</status>          
              <number>6</number>
      <!-- Comments -->       
       </list1>

     <list2>
      <entry>newEntry</entry>
      <output>thisOutput</output>
      <token>1</token>
              <status>0</status>          
              <number>1</number>
      <!-- Comments -->       
     </list2>

             <list3>
      <!-- repeat nodes as before -->         
     </list3>

任何帮助!
谢谢

Any help!!! Thanks

推荐答案

下面让批处理文件/保存所需的节点和过程中,他们每个父节点的值。这种方法允许仅通过更改一行在程序中修改的处理节点的数量和名称,它不使用任何外部命令(* .exe文件),也不呼叫命令,所以它是快速

The Batch file below get/save the values of the desired nodes and process they for each parent node. This method allows to modify the number and names of the processed nodes by just changing one line in the program and it does not use any external command (*.exe file) nor call command, so it is fast.

@echo off
setlocal EnableDelayedExpansion
rem Define the names of the desired nodes
set nodes=entry output token
rem Process file lines and get first two tokens separated by <> (ie: %%a=entry, %%b=myEntry)
for /F "tokens=2-3 delims=<>" %%a in (theXMLfile.xml) do (
   set "node=%%a"
   rem If this node is not the end of this record...
   if "!node:~0,5!" neq "/list" (
      rem If this node is one of the desired ones...
      if "!nodes:%%a=!" neq "%nodes%" (
         rem Assign this node into a variable of same name (ie: set entry=myEntry)
         set "%%a=%%b"
      )
   ) else (
      rem ListX node complete: process it, for example:
      ECHO call another.bat !entry! !output! !token!
   )
)

但是,如果输入文件是非常大,呼叫命令的每个的记录可以使程序花太长的时间执行。允许程序运行得更快的修改是存储的所有节点的数组中,而不是单个变量,然后通过父节点的数量被称为只有一次子程序。

However, if the input file is very large, the call command executed with each record may make the program to take too long. A modification that allows the program to run faster is to store all nodes in arrays, instead of individual variables, and then pass the number of parent nodes to the subroutine that is called just once.

@echo off
setlocal EnableDelayedExpansion
rem Define the names of the desired nodes
set nodes=entry output token
rem Define the index of the next array element
set index=1
rem Process file lines and get first two tokens separated by <> (ie: %%a=entry, %%b=myEntry)
for /F "tokens=2-3 delims=<>" %%a in (theXMLfile.xml) do (
   set "node=%%a"
   rem If this node is not the end of this record...
   if "!node:~0,5!" neq "/list" (
      rem If this node is one of the desired ones...
      if "!nodes:%%a=!" neq "%nodes%" (
         rem Assign this node into THE CURRENT ELEMENT OF AN ARRAY variable of same name (ie: set entry[!index!]=myEntry)
         set "%%a[!index!]=%%b"
      )
   ) else (
      rem ListX node complete: pass to next element of arrays
      set /A index+=1
   )
)
rem Call the subroutine and pass as parameter the number of parent listX nodes
set /A number=index-1
call :anotherSub %number%
goto :EOF

:anotherSub numberOfNodes
rem Process all nodes, for example:
for /L %%i in (1,1,%1) do (
   echo !entry[%%i]! !output[%%i]! !token[%%i]!
)
exit /B

安东尼奥

这篇关于使用批处理解析XML文件来获得某些特定节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆