0

I have below Unix shell script for inserting text file contents into an XML. I'm new to windows batch scripting. Can you suggest similar code in batch script to achieve the same ?

#Insert the contents of a text file into XML
a=0
rm $path/output.xml > /dev/null 2>&1
while IFS= read F1line
do
        b=`echo "$F1line" | tr -d " "`
        #echo $b
        if test "$b" == '<search string1>'
        then
                echo "$F1line" >> $path/output.xml
                while IFS= read F2line
                do
                echo "$F2line" >> $path/output.xml
                done < $path/insert.txt
                a=1
        fi

        if test $a -eq 1
        then
                if test "$b" = "<search string2>"
                then
                        #echo "$F1line" >> $path/output.xml
                        a=0
                else
                        continue
                fi
        fi

        echo "$F1line" >> $path/output.xml

done < $path/input.xml

echo "XML updated successfully"

Basically, the script starts writing line by line into output.xml (from input.xml) and when a specific string1 occurs, it starts inserting text file contents into xml and it continues to check for string2 and write remaining lines of input.xml into output.xml.

Regards, Sid

  • Hi Mofi, I'm fine with a powershell script as well. The lines are entered as part of '' and ''. Before that I'm doing trim of all spaces and assigning to a variable for validation purpose – Siddarth kaundinya Apr 09 '22 at 11:48
  • Avoid building or modifying [XML with string processes](https://stackoverflow.com/q/3034611/1422451) to avoid non well-formed XML. For instance, tags cannot have spaces in them. Such markup in fact is not XML. Use a language with compliant DOM libraries like aforementioned PowerShell. – Parfait Apr 09 '22 at 15:29
  • Sure Parfait. I have started working on power-shell script. Thanks very much for your inputs. – Siddarth kaundinya Apr 10 '22 at 03:15

0 Answers0