• CozWeAreFriends Banner. Nice isn't it?
  • CozWeAreFriends Banner. Nice isn't it?
  • Previous:
    Next:

    UNIX – Shell script to create XMLs based on user input data

    Posted on April 26th, 2012 by fath

    Hi All,

    I wrote a simple shell script that can read parameterized XML structure and will generate completed XMLs (with correct values) based on what we specified in the input data. This script was actually meant for my initial phase of Performance Testing data loading in my project.

    To accomplish this, I created 3 files which are the script itself, the parameterized XML template, as well as the data file. Below are the files:

    1) structure.txt (XML template) – here I replaced the value of the field that we need to parameterize with variable, such as ${ACC_NUM}

    <Sbsc xmlns=”http://www.cozwearefriends.com/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.cozwearefriends.com/XMLSchema XXX_Subsc
    ribers.xsd”>
    <ActSbsc id=”${ACC_NUM}”>
    <Act>
    <ActNo>${ACC_NUM}</ActNo>
    <Curr>${LOG}</Curr>
    <CustSegList/>
    <SubSta>A</SubSta>
    <BType>B</Btype>
    <Eff>2011-08-01T01:22:15-07:00</Eff>
    <CrtT>2011-08-01T01:22:15-07:00</CrtT>
    <ANArr elem=1>
    </Act>
    </ActSbsc>
    </Sbsc>

    2) createXML.sh (Shell script)

    # Script to create XML
    # based on user input
    # created by Fath

    STRUCTURE_FILE=structure.txt
    INPUT_FILE=input.txt
    OUTPUT_FILE=output.xml

    LOOP_NUMBER=`cat ${INPUT_FILE} | wc -l`

    i=1
    while [ “$i” -lt “$LOOP_NUMBER+1” ]
    do
    ACC_NUM=`head -${i} ${INPUT_FILE} | tail -1 | cut -f1 -d’;’`
    LOG=`head -${i} ${INPUT_FILE} | tail -1 | cut -f2 -d’;’`
    # Add in more variable here as well as in the command below if you have more than 2 parameters

    cat ${STRUCTURE_FILE} | sed -e s/’${ACC_NUM}’/${ACC_NUM}/g -e s/’${LOG}’/${LOG}/g >> ${OUTPUT_FILE}

    i=$((i+1))
    done

    3) input.txt (data file)

    ACC1;LOG1;
    ACC2;LOG2;
    ACC3;LOG3;

     

    You may change the files above depending on your requirements. Cheers. 🙂

     



    Leave a Reply