使用批处理文件从文件中删除 xml 个标签

remove xml tags from file with batch file

我有文件:

<tag1>text1</tag1><tag2>text2</tag2>
<tag3>text3</tag3>

我想成为:

text1
text2
text3

每个 text.Is 都可以换行吗?

@ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%a IN (q28433530.txt) DO (
 SET "line=%%a"
 CALL :process
)
)>newfile.txt
GOTO :EOF

:process

:procloop
FOR /f "tokens=1,2,3*delims=<>" %%i IN ("%line%") DO IF "%%j" neq "" ECHO(%%j&IF "%%l" neq "" SET "line=%%l"&GOTO procloop

GOTO :EOF

我使用了一个名为 q28433530.txt 的文件,其中包含您的数据用于我的测试。

产生newfile.txt

如果您给了我们具有现实代表性的数据,这可能会达到您想要的效果。

您可以将文件评估为 XML 并输出每个叶节点的 nodeValue.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set "xmlfile=test.xml"

@cscript /nologo /e:JScript "%~f0" "%xmlfile%" & goto :EOF

@end

var xmlDoc = WSH.CreateObject('Microsoft.XMLDOM'),
    fso = WSH.CreateObject('scripting.filesystemobject'),
    xmlFile = fso.OpenTextFile(WSH.Arguments(0), 1);

xmlDoc.loadXML('<root>' + xmlFile.ReadAll() + '</root>');

xmlFile.Close();
xmlDoc.async = false;

if (xmlDoc.parseError.errorCode) {
   var myErr = xmlDoc.parseError;
   WSH.Echo(myErr.reason);
} else {
    function walker(node) {
        for (var nodes=node.childNodes, i=0; i<nodes.length; i++) {
            if (nodes[i].hasChildNodes) {
                walker(nodes[i]);
            } else {
                WSH.Echo(nodes[i].nodeValue);
            }
        }
    }
    walker(xmlDoc.documentElement);
}