使用BAT编辑XML [英] Editing XML with BAT

查看:99
本文介绍了使用BAT编辑XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过BAT修改XML文件(实际上只是一个属性的值,版本).以下是我的XML文件的简短示例:

I'm trying to modify XML file (actually just a single attribute's value, Version) through BAT. The following is a short sample of my XML file:

<?xml version="1.0" encoding="utf-8"?>
<APPS>
    <APP Version="Some.Value">
        <!-- APP icon. -->
        <Icon>C:\Program Files (x86)\Somewhere</Icon>
        <Url>http://www.somewhere.com</Url>
    </APP>
</APPS>

以下是BAT文件:

@ECHO off
SETLOCAL DISABLEDELAYEDEXPANSION
SET anotherVariable=New.Value

(FOR /F "usebackq delims=" %%a IN (Sample.xml) DO (
   SET "line=%%a"
   SETLOCAL ENABLEDELAYEDEXPANSION
   SET "newLine=!line:    <APP =!"
   IF "!newLine!" neq "!line!" (
      SET "newLine=    <APP Version="%anotherVariable%">"
      ECHO.!newLine!
      ENDLOCAL
   ) ELSE (
       SET "line=!line:*:=!"
       ECHO.!line!
       ENDLOCAL
   )
)) > SampleOut.xml

但是以下是SampleOut.xml:

But the following is a SampleOut.xml:

<?xml version="1.0" encoding="utf-8"?>
<APPS>
    <APP Version="New.Value">
        <!-- APP icon. -->
\Program Files (x86)\Somewhere</Icon>
//www.somewhere.com</Url>
    </APP>
</APPS>

问题在于这些与路径相关的XML行(Icon和Url标记)已损坏.我希望有人能帮助我.

The problem is with these path related XML lines (Icon and Url tags) being corrupted. I hope someone can help me out.

推荐答案

对此进行测试:

type file.xml | repl "(<APP Version=\q).*(\q>)" "$1New.Value$2" xi >newfile.xml

这使用名为 repl.bat 的帮助程序批处理文件(由dbenham提供)-从以下位置下载:

This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

repl.bat 放置在与批处理文件相同的文件夹中,或者放置在路径上的文件夹中.

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

这应保留注释中讨论的以下属性:

This should preserve the following attributes as discussed in the comments:

type file.xml | repl "(<APP Version=\q).*?(\q.*>)" "$1New.Value$2" xi >newfile.xml

这篇关于使用BAT编辑XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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