Page 1 of 2
Find and Replace in XML
Posted: 21 Jul 2016 12:46
by SIMMS7400
Hello -
I have a need to find a certain string in an XML file and then replace with the the value set with a variable.
For instance, my batch script has to variables:
SET LCM_USER=admin
SET LCM_PSSWRD=welcome1
Here is my xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="" password=""/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
So, upon transformation, I need those values set in the variables above propogated to my XML.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="admin" password="welcome1"/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
I'm having trouble with the leading spaces and can't seem to get anything to work correctly. Any ideas?
Thanks, folks!
Re: Find and Replace in XML
Posted: 21 Jul 2016 13:21
by aGerman
I recomment to use the XML DOM rather than any kind of regex replacements. A JScript hybrid script may help out.
Code: Select all
if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
set "xmlfile=test.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
Re: Find and Replace in XML
Posted: 21 Jul 2016 13:34
by penpen
This may help (section 3.):
http://www.dostips.com/forum/viewtopic.php?p=34194#p34194You probably only need to replace the xslt part in it, with something like this (untested: sorry actually i've not much time):
Code: Select all
<xslt id="default">
<!-- <?xml version="1.0" encoding="utf-8"?> -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="priority|clntId">
<xsl:value-of select="."/>
<xsl:value-of select="'
'" disable-output-escaping="no"/>
</xsl:template>
</xsl:stylesheet>
</xslt>
penpen
Re: Find and Replace in XML
Posted: 21 Jul 2016 13:43
by SIMMS7400
Thank you, both!
Germin - I'm getting an error saying "Conditional compilation turned off".
Is there a special way to set up that ode for it to work in my batch file?
Thanks!
Re: Find and Replace in XML
Posted: 21 Jul 2016 13:46
by Squashman
SIMMS7400 wrote:Is there a special way to set up that ode for it to work in my batch file?
You put all your batch code inside the JSCRIPT comment.
Re: Find and Replace in XML
Posted: 21 Jul 2016 13:58
by SIMMS7400
This is what I did but it gives me that error:
Code: Select all
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
CALL C:\Windows\System32\cscript.exe //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0))
Thanks, I"m probably doing it wrong!
Re: Find and Replace in XML
Posted: 21 Jul 2016 14:15
by Squashman
This works for me.
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
@echo off &setlocal
echo ********************************************************
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
echo ********************************************************
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript.exe //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Re: Find and Replace in XML
Posted: 21 Jul 2016 14:19
by aGerman
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
... is the only valid line in both Batch and JScript. It exisits in order to introduce a JScript comment and must be the first line in your code.
Code: Select all
@if (@a)==(@b) @end /* Batch portion
@echo off &setlocal
set "logfile=mylog.txt"
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause
exit /b
JScript portion */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
Re: Find and Replace in XML
Posted: 21 Jul 2016 14:40
by SIMMS7400
Ah, so this cant reside in the middle portion of my script? ah i see. Let me test a blank .cmd.
Re: Find and Replace in XML
Posted: 21 Jul 2016 15:06
by aGerman
Very first line:
Code: Select all
@if (@a)==(@b) @end /* Batch portion:
Now include your whole Batch code that also contains the cscript call.
Finally append:
Code: Select all
exit /b
JScript portion */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0));
Regards
aGerman
Re: Find and Replace in XML
Posted: 21 Jul 2016 15:21
by Squashman
SIMMS7400 wrote:Ah, so this cant reside in the middle portion of my script?
That is what I meant by my first comment.
Re: Find and Replace in XML
Posted: 21 Jul 2016 17:17
by SIMMS7400
aGerman -
Thank you so much! I've been able to use a blank .cmd and get this work perfectly!!! However, when I try to incorporate this into my batch file I'm in the middle of building, it still fails. Can you help me place your portion in the correct areas?
Here is what I have right now:
Code: Select all
@IF (@a)==(@b) @end /* Batch portion
@ECHO off &setlocal
REM ------------------------------------------------------------------------
REM -- SCRIPT NAME: CREATE_APP_LCM_MIGRATE.cmd
REM ------------------------------------------------------------------------
REM --Call Environment Script--
call C:\Hyperion_Batch\Scripts\batch\_env.cmd
REM --Set working directory as script path--
cd /d %~dp0
REM ------------------------------------------------------------------------
REM SET LOG & ERROR PATHS
REM ------------------------------------------------------------------------
SET intrapath=%MAINPATH%%LOGPATH%%GENJBAPPLOGS%
SET errorintrapath=%MAINPATH%%ERRORPATH%%GENJBAPPERRORS%
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)
REM ------------------------------------------------------------------------
REM SET & DELETE LOG FILES
REM ------------------------------------------------------------------------
SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
if exist %logfile% del %logfile%
if exist %errorfile% del %errorfile%
REM -- Invoke Batch Client
echo ********************************************************>>%logfile%
echo %~n0 at %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%
set "xmlfile=Export.xml"
set "LCM_USER=admin"
set "LCM_PSSWRD=welcome1"
cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
PAUSE
SET myError1=%errorlevel%
IF %myError1%==0 goto EXECUTE_LCM_EXPORT
echo ********************************************************>>%logfile%
echo Error Encountered in PREPARE LCM EXPORT.XML FILE >>%logfile%
echo ********************************************************>>%logfile%
goto AbnormalExit
:EXECUTE_LCM_EXPORT
echo ********************************************************>>%logfile%
echo EXECUTE LCM EXPORT FOR %APP% >>%logfile%
echo ********************************************************>>%logfile%
CALL %LCM_UTIL_PATH%Utility.bat" %LCM_IE_PATH%%LCM_APP_DIR%Export.xml
SET myError1=%errorlevel%
IF %myError1%==0 goto NormalExit
echo ********************************************************>>%logfile%
echo Error Encountered in EXECUTE LCM EXPORT FOR %APP% >>%logfile%
echo ********************************************************>>%logfile%
goto AbnormalExit
:NormalExit
echo ********************************************************>>%logfile%
echo %~n0 Success >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0 >>%logfile%
echo ********************************************************>>%logfile%
date /t >>%logfile%
time /t >>%logfile%
EXIT /B 0
JScript portion: */
var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode = oXmlDoc.documentElement.selectSingleNode('//User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));
oXmlDoc.save(WScript.Arguments(0))
:AbnormalExit
echo ********************************************************>>%errorfile%
echo %~n0 results in Abnormal Exit >>%errorfile%
echo ********************************************************>>%errorfile%
echo Please Check the log file for errors>>%errorfile%
echo ********************************************************>>%errorfile%
echo Abnormal Exit - %~nx0 >>%errorfile%
echo ********************************************************>>%errorfile%
date /t >>%errorfile%
time /t >>%errorfile%
EXIT 1
But unfortunately, still fails. Thanks guys.
Re: Find and Replace in XML
Posted: 21 Jul 2016 18:25
by SIMMS7400
Nevermind, I got it folks!
Thank you again!! This is great!
Re: Find and Replace in XML
Posted: 22 Jul 2016 12:18
by SIMMS7400
Hi Guys -
I just have another question. I have a need to modify two more strings, both with the same value.
Here is the file:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Package>
<LOCALE>en_US</LOCALE>
<User name="" password=""/>
<Task>
<Source type="Application" product="HP" project="Default Application Group" application="GENJBAPP"/>
<Target type="FileSystem" filePath="/HP-GENJBAPP"/>
<Artifact recursive="true" parentPath="/Configuration" pattern="*"/>
<Artifact recursive="true" parentPath="/Essbase Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Global Artifacts" pattern="*"/>
<Artifact recursive="true" parentPath="/Plan Type" pattern="*"/>
<Artifact recursive="true" parentPath="/Relational Data" pattern="*"/>
<Artifact recursive="true" parentPath="/Security" pattern="*"/>
</Task>
</Package>
I need to change GENJBAPP (they are located in lines 6 & 7) to a value I specify from a variable, like we did above. Can you help me modify the above solution?
Thank you!
Re: Find and Replace in XML
Posted: 22 Jul 2016 12:21
by Squashman
SIMMS7400 wrote:I need to change GENJBAPP (they are located in lines 6 & 7) to a value I specify from a variable, like we did above. Can you help me modify the above solution?
Thank you!
I would think given the example you have already been given you could at least attempt to replicate that. Come back and show us the code you attempted.