gpt4 book ai didi

batch-file - 用批处理文件中的特殊字符替换标签

转载 作者:行者123 更新时间:2023-12-02 04:56:10 28 4
gpt4 key购买 nike

我有一个包含 xml 页眉和 xml 页脚的 xml 文件,我想删除页眉和页脚并将内容存储在一个变量中。 xml 文件的内容在循环内更改。

例如:

for (some range) do (
set "xmlHeader=<?xml version="1.0" encoding="UTF-8"?><Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">"
set "xmlFooter=</Config>"

<then get and set variable from xml file>
)

xml文件包含:

<?xml version="1.0" encoding="UTF-8"?><Config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<tag1>info1 changes in the loop</tag1>
<tag2>info2 changes in the loop</tag2>
</Config>

我想批量存储一个变量

<tag1>info1</tag1>
<tag2>info2</tag2>

我试过:

for (outer loop condition ) do (
for /f "Tokens=* Delims=" %%c in (xmlFile.xml) do set config=%%c
"!config:%xmlHeader%=!"
echo "!config!"
)

但是替换没有做任何事情。请帮忙!谢谢。

最佳答案

试试 sed for Windows :

例子:

    >type file
<?xml version="1.0" encoding="UTF-8"?><Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<tag1>info1 changes in the loop</tag1>
<tag2>info2 changes in the loop</tag2>
</Config>

>sed -r "\#xml version|</Config>#d; s#^(<[^>]+>\S+)[^<]+(<[^>]+>)#\1\2#" file
<tag1>info1</tag1>
<tag2>info2</tag2>

关于batch-file - 用批处理文件中的特殊字符替换标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17934128/

28 4 0