houngtze
houngtze

Reputation: 3

remove xml tags from file with batch file

I have file:

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

I want to be :

text1
text2
text3

with new line for every text.Is it possible with batch?

Upvotes: 0

Views: 1592

Answers (2)

Magoo
Magoo

Reputation: 80023

@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

I used a file named q28433530.txt containing your data for my testing.

Produces newfile.txt

This would probably do what you appear to want, provied you've given us realistic representative data.

Upvotes: 3

rojo
rojo

Reputation: 24466

You could evaluate the file as XML and output each leaf node's 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);
}

Upvotes: 2

Related Questions