首页 > 解决方案 > 通过搜索 id 使用批处理脚本删除 xml 标记的文本数据中的一大块行

问题描述

我想通过匹配 id 来删除 XML 标记的完整部分:

<?xml version="1.0"?>
<messages>
  <note ID="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
  </note>
  <note ID="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not!</body>
  </note> 

我的批处理代码是:

@ECHO OFF
title Delete Data
echo Press the id that needs to be Deleted
set /p id= 

echo hi

set notepad_program=C:\Windows\System32\notepad.exe
set notepad_file=C:\Users\shubham1.mishra\Desktop\Employee

cd "%notepad_file%"

rem for /F "tokens=*" %%A in (Edit_sample_xml.txt) do 
rem if %%A equ "502" (type Edit_sample_xml.txt | findstr /v %id% > Xml2.txt %%A)  

type Edit_sample_xml.txt | findstr /v %id% > Xml2.txt


start "%notepad_program%" "Xml2.txt"

pause

出口

我的代码只删除了与 id 匹配的一行,但我想删除完整的块,即具有该特定 id 的所有数据的完整“note”标签。

标签: xmlbatch-file

解决方案


推荐阅读