Replace node value in multiple xml files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Roy
Posts: 10
Joined: 31 Aug 2018 14:45

Replace node value in multiple xml files

#1 Post by Roy » 05 Nov 2018 15:07

Hello,
I am trying to replace some values at once (red ones) with each file's name in several .xml files inside a directory.

Example:

File name = 20181105abc.xml

<id>abcdef</id> <-- 20181105abc.xml
<city>London</city>
<country>UK</country>
<year>2018</year>
<type>Business trip</type>
<fileid>123456</fileid> <-- 20181105abc.xml

The batch script i've found so far and still testing is as below:

Code: Select all

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "replace=id"
set "replace2=fileid"
for %%P in ("*.xml") do (
    for /F "delims=" %%I in ('
        findstr /N /R "^" "%%~P" ^& ^> "%%~fP" break
    ') do (
        set "LINE=%%I"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*:=!"
        if defined LINE set "LINE=!LINE:%replace%=%replace%%%~nP</%replace%>!"
        if defined LINE set "LINE=!LINE:%replace%=%replace2%%%~nP</%replace2%>!"
        >> "%%~fP" echo(!LINE!
        endlocal
    )
)
endlocal
exit /B
The problem is that it only edits the first mentioned line and it doesn't replace each affected line. How can i replace them both ?
Any suggestions are welcome.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Replace node value in multiple xml files

#2 Post by ShadowThief » 05 Nov 2018 16:36

That code and sample data gives me the output

Code: Select all

<fileiddata</fileid>data</fileiddata</fileid>>>abcdef</fileiddata</fileid>data</fileiddata</fileid>>>
<city>London</city>
<country>UK</country>
<year>2018</year>
<type>Business trip</type>
<filefileiddata</fileid>data</fileiddata</fileid>>>123456</filefileiddata</fileid>data</fileiddata</fileid>>>
That said, shouldn't your second if defined LINE line read

Code: Select all

if defined LINE set "LINE=!LINE:%replace2%=%replace2%%%~nP</%replace2%>!"

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Replace node value in multiple xml files

#3 Post by Squashman » 05 Nov 2018 18:16

You should using a scripting language that has a native method to read and write XML. Vbscript, Jscript or Powershell.

Roy
Posts: 10
Joined: 31 Aug 2018 14:45

Re: Replace node value in multiple xml files

#4 Post by Roy » 06 Nov 2018 02:29

Is there any way to replace the affected line/lines with the following? %replace%%%~nP</%replace%>!

E.g.
<id>20181105abc.xml</id>

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Replace node value in multiple xml files

#5 Post by npocmaka_ » 06 Nov 2018 04:45

We need a general purpose .bat that can handle xml files :!:

Here's how the xmls can be handled with WSH - https://docs.microsoft.com/en-us/previo ... v=msdn.10)
.net is more powerful though

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Replace node value in multiple xml files

#6 Post by npocmaka_ » 06 Nov 2018 05:03

here's a .bat script that does the thing you want with a single file (you can call it from another bat that iterates the needed files):

Code: Select all

@if (@X)==(@Y) @end /* JScript comment 
        @echo off        
        cscript //E:JScript //nologo "%~f0" %* 
		::pause
        exit /b %errorlevel%       
@if (@X)==(@Y) @end JScript comment */ 

var ARGS=WScript.Arguments;
var xml=ARGS.Item(0);

var fso=new ActiveXObject("Scripting.FileSystemObject")	
var XMLO = new ActiveXObject('Microsoft.XMLDOM');
XMLO.async = "False";
var file=fso.GetFile(xml);
var filename=fso.GetFileName(file);

// --xpaths--
var id="//id";
var fileid="//fileid"


updateXml(xml,id,filename,"text");
updateXml(xml,fileid,filename,"text");


function updateXml(file,xpath,newVal,attribute) {
	var err="";
	try {
		var load=XMLO.load(decodeURI(file));
	} catch (error) {
		err=error.message;
	}
			
	if (load){
		var node=XMLO.selectSingleNode(xpath);
		node[attribute]=newVal;
		XMLO.save(file);
	} else {
		alert ("Failed to load file: " + file + " " + err);
	}
}

Though we still need a general purpose xml.bat :!:

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Replace node value in multiple xml files

#7 Post by penpen » 08 Nov 2018 10:35

You might want to do xslt, see example here:
viewtopic.php?p=32941#p32941

penpen

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Replace node value in multiple xml files

#8 Post by npocmaka_ » 09 Nov 2018 07:22

penpen wrote:
08 Nov 2018 10:35
You might want to do xslt, see example here:
viewtopic.php?p=32941#p32941

penpen
Thanks.

Post Reply