使用批处理脚本替换xml文件中的字符串

时间:2022-01-05 02:19:29

I'm new to batch script. I want to replace a strings in a particular file. In below script I'm getting error.

我是批处理脚本的新手。我想替换特定文件中的字符串。在下面的脚本我收到错误。

@echo off
$standalone = Get-Content 'C:\wildfly\standalone\configuration\standalone.xml'

$standalone -replace '<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>','<wsdl-host>${jboss.bind.address:0.0.0.0}</wsdl-host>' | 
Set-Content 'C:\wildfly\standalone\configuration\standalone.xml' 

1 个解决方案

#1


1  

The proper way to edit XML is to process it as an XML document, not as a string. That's because the XML file is not guaranteed to maintain specific formatting. Any edits should be context-aware and string replace isn't. Consider the three eqvivalent XML fragments:

编辑XML的正确方法是将其作为XML文档处理,而不是作为字符串处理。那是因为不保证XML文件能够保持特定的格式。任何编辑都应该是上下文感知的,而字符串替换则不是。考虑三个eqvivalent XML片段:

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host >

<wsdl-host >${jboss.bind.address:127.0.0.1}</wsdl-host >

Note that whitespacing in element names is different and it's legal to add some. What's more, in practice, a lot of implementations simply discard line breaks in element values, so the two following are likely to provide same results to a config parser:

请注意,元素名称中的whitespacing是不同的,添加一些是合法的。更重要的是,在实践中,许多实现只是丢弃元素值中的换行符,因此以下两个可能会为配置解析器提供相同的结果:

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>

<wsdl-host>${jboss.bind.address:127.0.0.1}
</wsdl-host>

It really doesn't make much sense to process XML as string, does it?

将XML作为字符串处理真的没有多大意义,是吗?

Fortunately, Powershell has built-in support for XML files. A simple approach is like so,

幸运的是,Powershell内置了对XML文件的支持。一个简单的方法是这样的,

# Mock XML config
[xml]$x = @'
<root>
<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>
</root>
'@

# Let's change the wsdl-host element's contents
$x.root.'wsdl-host' = '${jboss.bind.address:0.0.0.0}'

# Save the modified document to console to see the change
$x.save([console]::out)

<?xml version="1.0" encoding="ibm850"?>
<root>
  <wsdl-host>${jboss.bind.address:0.0.0.0}</wsdl-host>
</root>

If you can't use Powershell and are stuck with batch scripts, you really need to use a 3rd party XML manipulation program.

如果您不能使用Powershell并且无法使用批处理脚本,那么您确实需要使用第三方XML操作程序。

#1


1  

The proper way to edit XML is to process it as an XML document, not as a string. That's because the XML file is not guaranteed to maintain specific formatting. Any edits should be context-aware and string replace isn't. Consider the three eqvivalent XML fragments:

编辑XML的正确方法是将其作为XML文档处理,而不是作为字符串处理。那是因为不保证XML文件能够保持特定的格式。任何编辑都应该是上下文感知的,而字符串替换则不是。考虑三个eqvivalent XML片段:

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host >

<wsdl-host >${jboss.bind.address:127.0.0.1}</wsdl-host >

Note that whitespacing in element names is different and it's legal to add some. What's more, in practice, a lot of implementations simply discard line breaks in element values, so the two following are likely to provide same results to a config parser:

请注意,元素名称中的whitespacing是不同的,添加一些是合法的。更重要的是,在实践中,许多实现只是丢弃元素值中的换行符,因此以下两个可能会为配置解析器提供相同的结果:

<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>

<wsdl-host>${jboss.bind.address:127.0.0.1}
</wsdl-host>

It really doesn't make much sense to process XML as string, does it?

将XML作为字符串处理真的没有多大意义,是吗?

Fortunately, Powershell has built-in support for XML files. A simple approach is like so,

幸运的是,Powershell内置了对XML文件的支持。一个简单的方法是这样的,

# Mock XML config
[xml]$x = @'
<root>
<wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>
</root>
'@

# Let's change the wsdl-host element's contents
$x.root.'wsdl-host' = '${jboss.bind.address:0.0.0.0}'

# Save the modified document to console to see the change
$x.save([console]::out)

<?xml version="1.0" encoding="ibm850"?>
<root>
  <wsdl-host>${jboss.bind.address:0.0.0.0}</wsdl-host>
</root>

If you can't use Powershell and are stuck with batch scripts, you really need to use a 3rd party XML manipulation program.

如果您不能使用Powershell并且无法使用批处理脚本,那么您确实需要使用第三方XML操作程序。