使用CMD批处理解析\编辑XML值

时间:2016-09-28 07:56:14

标签: xml parsing batch-file

我需要读取并可能编辑XML文件中标记之间的值。我目前仅限于使用CMD批处理文件作为唯一的处理方式(将来可能会转移到power-shell)。 "示例File.xml"总是在相同的布局中,下面显示了一个完整的文件。

感兴趣的标签是<MountPoint>\\.\PHYSICALDRIVE6</MountPoint>。我需要读取物理驱动器号(在此示例中为6)并根据其值进行编辑。出于这个原因,我想保持批处理的读取和编辑组件分开。任何帮助都会受到赞赏,因为我的批处理技能不合适而且我还没有接近。

Mounting in progress, wait...
<MIPResponse Command="VIEW" Version="1.0">
<Drive>
  <IsActive>F</IsActive>
  <DeviceType>PhysicalDrive</DeviceType>
  <PartitionType>Physical</PartitionType>
  <MountPoint>\\.\PHYSICALDRIVE6</MountPoint>
  <Capacity>80026361344</Capacity>
  <CapacityString>74.53 GB</CapacityString>
<Partition>
  <IsActive>T</IsActive>
  <PartitionType>NTFS/OS2 HPFS/exFAT</PartitionType>
  <MountPoint></MountPoint>
  <Location>1048576</Location>
  <Capacity>104857600</Capacity>
  <CapacityString>100.0 MB</CapacityString>
</Partition>
<Partition>
  <IsActive>F</IsActive>
  <PartitionType>NTFS/OS2 HPFS/exFAT</PartitionType>
  <MountPoint></MountPoint>
  <Location>105906176</Location>
  <Capacity>79919316992</Capacity>
  <CapacityString>74.43 GB</CapacityString>
</Partition>
</Drive>
</MIPResponse>

1 个答案:

答案 0 :(得分:1)

ECHO Matching in file...
setlocal enableextensions disabledelayedexpansion
set "Result="
for /f "tokens=3 delims=<>" %%a in (
    'find /i "<MountPoint>\\.\PHYSICALDRIVE" ^< "The Sample File.xml"'
) do set "Result=%%a"
set Result=%Result:~-1%
ECHO Found Value:  PHYSICALDRIVE%Result%

ECHO Updating the other file...
powershell -Command "(gc 'The Other File.xml') -replace '"PHYSICALDRIVE[0-9]{1,2}"', '"PHYSICALDRIVE%Result%"' | Out-File -Encoding "ASCII"  'The Other File.xml'"