01 August 2011 ~ 0 Comments

DOS Batch XML Editor / Append Node

REM ## this is a super handy script that works as a Batch XML Editor. I can take any large group of XML files, and add a new dynamic node, switching out the variables by using the file name. You could switch out using file name for some other variable, or other ones that you set elsewhere - but for my purposes this was exactly what I needed to do. I didn't find many good batch XML editing things online, especially where I could use my name variables to write unique XML to each of the  100+ files I was dealing with. Hand editing XML is a nightmare, and prone to errors galore, so scripts like this can be a life saver. To use it, place all of your XML files in a subfolder /xml. Make sure the names of these files match up to the values you are trying to write, or change the XML node values below to something static. By Stacey Tipton Reiman, www.akwebgenius.com/blog ##

@echo off

setlocal ENABLEDELAYEDEXPANSION

REM delete the new xml folder is we are running this for the 2nd time
if exist new_xml del new_xml

REM create a folder for our new updated XML files
if not exist new_xml mkdir new_xml
if exist xml_list.txt del xml_list.txt

REM find all of the XML files within the xml\ folder
FOR /F "tokens=1,2 delims=." %%A in ( 'dir xml\  /O:N /L /B  /N *.xml') do (

set /A Cnt+=1

REM this will take all of the names of the XML files and create variables, which will be used in all of the new XML files so that they contain the correct information. NOTE!! Your file names need to be the same as the variables you want to use in your nodes! In my case, my lessons are all named like colors, body_parts, numbers, etc - so the xml files are colors.xml, body_parts.xml, numbers.xml, etc. Then this value is used within the new crossword XML node.

set filename=%%A

set fileout=new_xml\!filename!.xml
set filelist=xml_list.txt

REM this is the node I am appending my new xml node BEFORE... - replace with whatever unique node you want to search and append in your own XML files

findstr  /v "</lessons>" xml\!filename!.xml > !fileout!
rem findstr  /v "</param>" !filename!.xml > !fileout2!

REM this is my new node, appended to all of the 100+ XML files in my folder

echo ^<row lessonTitle="Crossword Puzzle" fileName="crossword.swf" xmlPath="your_xml_path/lessons/!filename!/xml/crossword.xml" tutorFile="assets/images/pic.swf" lessonIcon="crossword" activityId="!filename!crossword" completed="False" type="activity"/^> >> !fileout!
echo  ^</lessons^>  >> !fileout!
rem echo  ^<value^>%1^</value^>  >> !fileout2!

echo Processing the XML file !filename!.xml
echo !Cnt!
echo !filename! >> !filelist!

)

echo AWESOME^^!...I just created !Cnt! new XML Files!
echo !Cnt! >> !filelist!
pause
cls
endlocal
echo GREAT check the filelist.txt file to see the names of your XML files and COUNT
type xml_list.txt
pause
Tags: , , , , ,

Leave a Reply


5 - one =