Find and Replace in XML

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SIMMS7400
Posts: 544
Joined: 07 Jan 2016 07:47

Re: Find and Replace in XML

#16 Post by SIMMS7400 » 22 Jul 2016 12:30

The error it says is "Out of range"

Code: Select all

@if (@a)==(@b) @end /* Batch portion

@echo off &setlocal
set "logfile=mylog.txt"
call C:\Hyperion_Batch\Scripts\batch\_env.cmd
echo ********************************************************>>%logfile%
echo  PREPARE LCM EXPORT.XML FILE AT %TIME%                  >>%logfile%
echo ********************************************************>>%logfile%
echo  PREPARE LCM EXPORT.XML FILE AT %TIME%

SET LCM_APP_DIR=LCM_GENJBAPP\
SET NEW_APP_NAME=TESTZ

set "xmlfile=%LCM_IE_PATH%%LCM_APP_DIR%Export.xml"
set "NEW_A_N=%NEW_APP_NAME%"

cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%NEW_APP_NAME%"
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));

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

Re: Find and Replace in XML

#17 Post by Squashman » 22 Jul 2016 12:35

SIMMS7400 wrote:The error it says is "Out of range"

That is not all the error message says. Why do you think that entire error message is not important. It tells you exactly the line the error is on. DETAILS DETAILS DETAILS!!!!!!!!!!

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

Re: Find and Replace in XML

#18 Post by Squashman » 22 Jul 2016 12:46

SIMMS7400 wrote:

Code: Select all

var oNode = oXmlDoc.documentElement.selectSingleNode('User');
oNode.setAttribute('name', WScript.Arguments(1));
oNode.setAttribute('password', WScript.Arguments(2));

Don't you think you might need to change or add more of these lines with the correct information.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and Replace in XML

#19 Post by aGerman » 22 Jul 2016 14:57

"Out of range"

Of course. You passed only two arguments ("%xmlfile%" "%NEW_APP_NAME%"). The JScript code expects 3 arguments. Thus, accessing index 0 and 1 succeeds but as soon as you try to access WScript.Arguments(2) it will fail.
As Sqashman said you have to cusomize the JScript code accordingly. Currently I don't understand whether or not you want to change "name", "password", "application", and "filePath" attributes with only one cscript call.

Regards
aGerman

SIMMS7400
Posts: 544
Joined: 07 Jan 2016 07:47

Re: Find and Replace in XML

#20 Post by SIMMS7400 » 22 Jul 2016 15:27

Hi aGerman -

Currently, I want to keep the code you provided me yesterday as is.

However, in the middle of my script I need to reference the Export.xml again and change the GENJBAPP strings. I want to keep name, password, and filepath as is.

Application will be introduced in the patter part of my script and will require it changed in the two locations of my XML as shown above.

This is my intention:

Code: Select all

@if (@a)==(@b) @end /* Batch portion

@echo off &setlocal
set "logfile=mylog.txt"

call C:\Hyperion_Batch\Scripts\batch\_env.cmd
echo ********************************************************>>%logfile%
echo PREPARE LCM EXPORT.XML FILE AT %TIME%                   >>%logfile%
echo ********************************************************>>%logfile%

set "xmlfile=%LCM_IE_PATH%%MIG_DEF_DIR%\Export.xml"
set "LCM_U=%LCM_USER%"
set "LCM_P=%LCM_PSSWRD%"

cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%LCM_USER%" "%LCM_PSSWRD%"
pause

echo ********************************************************>>%logfile%
echo EXECUTE LCM EXPORT FOR %LCM_APP%                        >>%logfile%
echo ********************************************************>>%logfile%

CALL %LCM_UTIL_PATH%Utility.bat" %LCM_IE_PATH%%LCM_APP_DIR%Export.xml

echo ********************************************************>>%logfile%
echo PREPARE NEW MIGRATION DEFINITION DIRECTORY              >>%logfile%
echo ********************************************************>>%logfile%
echo    PREPARE NEW MIGRATION DEFINITION DIRECTORY
echo.
echo    PLEASE PROVIDE A NAME FOR THE NEW APPLICATION TO BE IMPORTED
SET /p NEW_APP_NAME=

SET NEW_MIG_DEF_DIR1=\LCM_%NEW_APP_NAME%

echo.
echo    THE NEW APPLICATION NAME IS : "%NEW_APP_NAME%"
echo.

XCOPY /E /S /Y %LCM_IE_PATH%%MIG_DEF_DIR% %LCM_IE_PATH%%NEW_MIG_DEF_DIR1%
REN %LCM_IE_PATH%%NEW_MIG_DEF_DIR1%\HP-%LCM_APP% HP-%NEW_APP_NAME%


echo ********************************************************>>%logfile%
echo REPLACE *.XML FILES WITH %NEW_APP_NAME%                 >>%logfile%
echo ********************************************************>>%logfile%

SET APP_NAME_2=TEST

set "xmlfile=%LCM_IE_PATH%%MIG_DEF_DIR%\Export.xml"
set "LCM_AN2=%APP_NAME_2%"

cscript //nologo //e:jscript "%~fs0" "%xmlfile%" "%APP_NAME_2%"

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));
var oNode = oXmlDoc.documentElement.selectSingleNode('Source');
oNode.setAttribute('app', WScript.Arguments(1));
oXmlDoc.save(WScript.Arguments(0));


So, the second second of my JScript should change both GENJBAPP strings to TEST.

Is that clearer?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and Replace in XML

#21 Post by aGerman » 22 Jul 2016 15:59

Replace the JScript part of the code.

Code: Select all

JScript portion */
if (WScript.Arguments.Count() > 3 || WScript.Arguments.Count() < 2) {
  WScript.Quit();
}

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode;
if (WScript.Arguments.Count() == 3) {
  oNode = oXmlDoc.documentElement.selectSingleNode('User');
  oNode.setAttribute('name', WScript.Arguments(1));
  oNode.setAttribute('password', WScript.Arguments(2));
} else if (WScript.Arguments.Count() == 2) {
  oNode = oXmlDoc.documentElement.selectSingleNode('Task/Source');
  oNode.setAttribute('application', WScript.Arguments(1));
  oNode = oXmlDoc.documentElement.selectSingleNode('Task/Target');
  oNode.setAttribute('filePath', '/HP-' + WScript.Arguments(1));
}
oXmlDoc.save(WScript.Arguments(0));

Regards
aGerman

SIMMS7400
Posts: 544
Joined: 07 Jan 2016 07:47

Re: Find and Replace in XML

#22 Post by SIMMS7400 » 22 Jul 2016 19:47

aGerman -

Thank you so much! I will test this out and report back!

Thank you again have a great night!

SIMMS7400
Posts: 544
Joined: 07 Jan 2016 07:47

Re: Find and Replace in XML

#23 Post by SIMMS7400 » 23 Jul 2016 14:01

aGerman -

Thank you again for your help! I just executed the process with the latest additions and it seems were just off by a little.

Here is the before file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<Package>
    <LOCALE>en_US</LOCALE>
    <User name="" password=""/>
    <Task>
        <Source type="FileSystem" filePath="/HP-GENJBAPP"/>
        <Target type="Application" product="HP" project="Default Application Group" application="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>


The file after my script runs is below. Notice, the new application name (DOS_TIPS) is added, but it adds it with the 'application' and 'filepath' attributes set in the JScript section at the end while the original (GENJBAPP) string remains. Also notice the new application is added with the attributes on the opposite lines they should be. Is this an issue on my end?

* Issues are on lines 6 & 7 of the script in code tags *

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<Package>
   <LOCALE>en_US</LOCALE>
   <User name="admin" password="welcome1"/>
   <Task>
      <Source type="FileSystem" filePath="/HP-GENJBAPP" application="DOS_TIPS"/>
      <Target type="Application" product="HP" project="Default Application Group" application="GENJBAPP" filePath="/HP-DOS_TIPS"/>
      <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>

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and Replace in XML

#24 Post by aGerman » 23 Jul 2016 14:28

Have a look at the former XML codes that you posted. The "application" attribute belonged to the "Source" node and the "filePath" attribute belonged to the "Target" node. It's vice versa in your new code though.
Thus, the JScript part has to be changed again.

Code: Select all

JScript portion */
if (WScript.Arguments.Count() > 3 || WScript.Arguments.Count() < 2) {
  WScript.Quit();
}

var oXmlDoc = new ActiveXObject('Microsoft.XMLDOM');
oXmlDoc.load(WScript.Arguments(0));
var oNode;
if (WScript.Arguments.Count() == 3) {
  oNode = oXmlDoc.documentElement.selectSingleNode('User');
  oNode.setAttribute('name', WScript.Arguments(1));
  oNode.setAttribute('password', WScript.Arguments(2));
} else if (WScript.Arguments.Count() == 2) {
  oNode = oXmlDoc.documentElement.selectSingleNode('Task/Source');
  oNode.setAttribute('filePath', '/HP-' + WScript.Arguments(1));
  oNode = oXmlDoc.documentElement.selectSingleNode('Task/Target');
  oNode.setAttribute('application', WScript.Arguments(1));
}
oXmlDoc.save(WScript.Arguments(0));

FWIW That has been a very simple adjustment. It's always a good idea to (at least try to) understand the code instead of mindlessly c/p it.

Regards
aGerman

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: Find and Replace in XML

#25 Post by mirrormirror » 23 Jul 2016 18:25

Not to distract from the code discussion here but I've read this tool can assist in command-line XML work:

https://sourceforge.net/projects/xmlstar/files/

Some usage info. here:

http://xmlstar.sourceforge.net/docs.php

Post Reply