繁体   English   中英

批处理文件以提取特定XML标记的值

[英]A batch file to extract the value of a specific XML tag

* *我需要一个批处理文件,该文件仅检索数据标签值(不带标签名称)并将其记入.txt文件中。 该文件可能具有比列出的XML标签更多的XML标签。

因此输出应为:

资本收益是美国收入差距的关键因素,而获胜者背后的力量则占据了我们经济体系的全部口号。 如果您想要在美国平均赚钱,则必须提高15%的资本利得税。**

我的文件如下所示:**

<TABLE>
Table 30
<ROW>
Multiple Rows
<DATA>
Capital gains are the key ingredient of income disparity in the US-- and the force  
behind the winner takes all mantra of our economic system. If you want  even out 
earning power in the U.S, you have to raise the 15% capital gains tax.
</DATA>
</ROW>
</TABLE>

我没有Windows机器,所以请原谅语法稍有偏离,但是如果数据如示例中所列,这样的事情可能会有所帮助,尽管您可能要考虑使用Powershell,因为它具有出色的性能。用于处理XML的工具:

setlocal enabledelayedexpansion
set start_reading="0"
set stop_reading="0"
set your_file_name=%~1

if EXIST "%your_file_name%.txt" del "%your_file_name%.txt"

for /f "eol=; tokens=1 delims=" %%c in ('type "%your_file_name_here%.xml"') do (
  set line=%%c

  @REM Determine if at start of Data Tag
  for /f "eol=; tokens=1 delims=" %%d in ('echo !line! ^| findstr /i /c:"<DATA>"') do (
    set start_reading="1"
  )

  @REM Determine if at end of Data Tag
  for /f "eol=; tokens=1 delims=" %%d in ('echo !line! ^| findstr /i /c:"</DATA>"') do (
    set stop_reading="1"
  )

  @REM stop reading DATA tag input
  if "!stop_reading!"=="1" (
    set start_reading="0"
  )

  @REM skips first line assumed to be <DATA>
  if "!start_reading!"=="2" (
    echo !line! >> "%your_file_name_here%.txt"
  )

  @REM Ready to start reading post <DATA> line
  if "!start_reading!"=="1" (
    set stop_reading="0"
    set start_reading="2"
  )

)

@REM Check results
type "%your_file_name_here%.txt"

让我知道您是否需要帮助。 我不得不在DOS才能让我们使用的环境中工作,所以我感到您的痛苦。 祝好运! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM