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

时间:2015-02-10 14:00:06

标签: batch-file

我有档案:

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

我想成为:

text1
text2
text3

为每个文本添加新行。是否可以批量处理?

2 个答案:

答案 0 :(得分:3)

@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

这可能会做你想要的,证明你给了我们真实的代表性数据。

答案 1 :(得分:2)

您可以将文件评估为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);
}