0

Folks,

Trying to update the XML file values using 'sed'. bat command not recognized.

CODE:

**@echo off
SET PREFIX=ROOM107-
SET SUFFIX=\Admin
SET /P PCNAME1=Please enter your desired NUMBER:
SET PCNAME=%PREFIX%%PCNAME1%%SUFFIX%

sed -i "s#<UserId>Admin</UserId>#<UserId>%PCNAME%</UserId>#" changexml.xml

pause**

Any other way to update the below XML file using BAT command ?

XML:

<xml>
<Main_group>
     <group_1>
        <add key="A" value="AMERICA"/>
        <add key="B" value="BALL"/>
     </group_1>
     <group_2>
        <add key="A" value="AMERICA"/>
        <add key="B" value="BALL"/>
     </group_2>
 </Main_group>
 <SubGroup>
    <add key="A" value="AMERICA"/>
    <add key="B" value="BALL"/>
 </SubGroup>
 </xml>

trying to update the value of 'A' and 'B' of the node like below

<SubGroup>
    <add key="A" value="USA"/>
    <add key="B" value="Basket Ball"/>
 </SubGroup>
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Riv
  • 11
  • 3
  • `sed` is a *nix tool, you need to install it to MSWindows to be able to use it. However, `sed` is not a tool to process XML. – choroba Dec 20 '17 at 09:53
  • @Cyrus - thanks for the links, those links supports linux os. im looking for windows platform that too with BAT command – Riv Dec 20 '17 at 10:22
  • @choroba - Thanks for the info! – Riv Dec 20 '17 at 10:22
  • @Cyrus - Many threads bit confused . can you share some code or example here. will be great helpful – Riv Dec 20 '17 at 11:10
  • Are you sure you want to [**pͬ̎ͫa̠̹̙͐̂̊͂ͫ̾̌ͅr̥̓s̬̓̾̓̑e͔͓̝̗̼̙̞͑̏̇͛̿̀ Ẋ̴̟̼̱̱̲ͬ̒ͥ̈́ͤͅM̻̯̬̘̞̼̺̄͑ͧ͋̀̀L̘͚̳̬͇͎̻͊̅̄̑͌ ͚̭̠͙ͅw̳̖̫̘̔͐̏͘i̷͔̳̠͔͚͑ͬt̶͖̪͉͍̱ͨ̌́͂̎ͪh̋ͧ̋ͦ͏̱̭͚̗ ͉̖̫͕ͪ̓͘R̶͔͍͍̖̔ͯ̐̌eͣ͌̄͌ͤ͏̼̜̙̭͈g̝̘͕̋̑E͙͈̯̩̹ͥ̅x̟͔͉͕ͫͪ**](https://stackoverflow.com/a/1732454/5958455)? – iBug Dec 20 '17 at 11:18
  • @Cyrus - Thanks for the spontaneous help :) . please clarify my doubts below – Riv Dec 20 '17 at 11:29
  • @iBug - thanks for your help :) information its too complex for me. just a beginner will be really helpful for me if you share some code or examples ! – Riv Dec 20 '17 at 11:31
  • Vbscript, Jscript and Powershell are all scripting languages you can use in Windows and they all have native capability to read and write XML files. – Squashman Dec 20 '17 at 14:16

3 Answers3

1

Your xml was not valid so I've edited it a little bit (saved as xml.txt):

<xml>
    <Main_group>
         <group_1>
            <add key="A" value="AMERICA"/>
            <add key="B" value="BALL"/>
         </group_1>
         <group_2>
            <add key="A" value="AMERICA"/>
            <add key="B" value="BALL"/>
         </group_2>
     </Main_group>
     <SubGroup>
        <add key="A" value="AMERICA"/>
        <add key="B" value="BALL"/>
     </SubGroup>
 </xml>

Here's a bat file that edit his values (should be in the same directory).Without need of installing external programs and the xml is parsed as xml without using regex. You can change the hardcoded values:

@if (@X)==(@Y) @end /* JScript comment
    @echo off


    cscript //E:JScript //nologo "%~f0" 

    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */

//------------------------
// -- hard coded values --
var originalFile="xml.txt";
var newFile="xml1.xml";

var xpath1='//SubGroup/add[@key="A"]/@value';
var xpath2='//SubGroup/add[@key="B"]/@value';

var newValue1="USA";
var newValue2="Basket Ball";

//---------------------------

var objDoc;
var objNodes;
var loaded;

try {
    objDoc = WScript.CreateObject("MSXML.DOMDocument");
    loaded=objDoc.load(originalFile);
} catch (err){
    WScript.Echo("Error while parsing the xml");
    WScript.Echo(err.message);
    WScript.Quit(1);
}

if(!loaded){
    WScript.Echo("Error while parsing the xml");
    WScript.Echo("");
    WScript.Echo("Error Code:"+objDoc.parseError.errorCode);
    WScript.Echo("");
    WScript.Echo("Line:"+objDoc.parseError.line+" Posotion:"+objDoc.parseError.filepos);
    WScript.Echo("");
    WScript.Echo("Reason:"+objDoc.parseError.reason);
    WScript.Echo("");
    WScript.Echo("URL:"+objDoc.parseError.url);
    WScript.Echo("");
    WScript.Echo(objDoc.parseError.srcText);
    WScript.Quit(5);
}



var node1=objDoc.selectSingleNode(xpath1);
var node2=objDoc.selectSingleNode(xpath2);
node1.text=newValue1;
node2.text=newValue2;


objDoc.save(newFile);
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • im not able to execute sucessfully getting error Reason:Only one top level element is allowed in an XML document – Riv Dec 20 '17 at 14:00
  • @Riv - you can change the last line with `objDoc.save(originalFile);` – npocmaka Dec 20 '17 at 14:09
  • @Riv - for the first comments - it seems that your xml is not valid , you have two or more root elements. You can try for example to add one more root elements that hold everything. – npocmaka Dec 20 '17 at 14:11
1

You probably want to use xmlstarlet to handle XML documents.

Here is the link to install it: http://xmlstar.sourceforge.net/download.php

After getting it working, here is the code to handle the required changes:

xmlstarlet ed [--inplace] -u '/xml/SubGroup/add[@key="A"]/@value' -v "USA" -u '/xml/SubGroup/add[@key="B"]/@value' -v "Basket Ball" changexml.xml

The argument --inplace will change the original file.

Paulo Pedroso
  • 3,555
  • 2
  • 29
  • 34
0

If you want to use sed on Windows, you have to get a Unix environment like MSYS, Cygwin or Windows Subsystem for Linux.

However, it's not a good idea to parse XML with RegEx.

Compo
  • 36,585
  • 5
  • 27
  • 39
iBug
  • 35,554
  • 7
  • 89
  • 134