Parse an XML task list to create each task.xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse an XML task list to create each task.xml file
# 1  
Old 11-08-2008
Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as

Code:
<TASKLIST
    <TASK definition="Completion date" id="Taskname1" Some other 
         <CODE name="Code12" 
               <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter>
               <Parameter pname="Cat" input="cute" units="NA"      /Parameter>
          /CODE>
     /TASK>
<TASK definition="Completion date" id="Taskname2" Some other 
         <CODE name="Code3" 
          /CODE>
         <CODE name="Code2" 
           /CODE>
     /TASK>
/TASKLIST>

I need to parse the task list into seperate task xml files starting from the <TASK tag to the /TASK> tag using the TASK id for the *.xml name.

I have written a grep command to capture all id names and then a sed command along with another grep command to clean up everything except the id leaving me with a list of just Task id names. My code is not eloquant, but works.

Now I am stuck. Please help. I am a novice at Unix scripting.

Last edited by bakunin; 11-08-2008 at 06:29 PM.. Reason: Please use code-tags areoun code!
# 2  
Old 11-08-2008
Quote:
Originally Posted by MissI
I have written a grep command to capture all id names and then a sed command along with another grep command to clean up everything except the id leaving me with a list of just Task id names.
Just show us what you have got and we will work from there. Don't be shy, we dislike people relying on others doing their work for them much more then people not well versed in the art of scripting. The second is tolerable, the first is not.

bakunin
# 3  
Old 11-10-2008
hi,

below perl code should be ok, pls delete the last and first file of your xml file and run it.

Code:
$/="TASK>";
open FH,"<file";
while(<FH>){
        if(m/id="(.*)"/){
                $file=sprintf("%s.xml",$1);
                open FH1,">$file";
                print FH1 $_;
                close FH1;
        }
}
close FH;

# 4  
Old 11-11-2008
Bug

Thanks so much.

Have never worked with Perl before and downloaded tutorial to help me understand the syntax. Would you suggest a good book on it?

This code was awesome. The input file does consist of lots more information and the task tag has about 2 dozen attributes. (This is an embedded software file created by another integrator and has about 1000 tasks in it with multiple tags underneath). I do software code checking as code changes, this whole file was being recreated. Now all that has to be done is change the task xml file and re-imbed it. Magic..

The xml code is on an intranet machine and cannot be copied from, so the file and problem was abbreviated for convenience.

The only problem was

(m/id="(.*)"/)

actually captured the whole line including other attributes.

Each task is defined by the same version number appended to the end. I got the correct name by adding

(m/id="(.*)VERS5"/).

The last part was not captured, but it did end there. I tried the
(m/s/id="(.*)VERS5"/)

but it did not work. I got what I wanted by doing the perl then

ls *.xml > temp1
sed -e "s|.xml||g" temp1 > temp2
cat temp2 |xargs -n 1| while read TASKNAME
do
mv $TASKNAME.xml $TASKNAME'Vers5.xml'
done

This is the first time I have worked with Sun workstations and don't have this scripting thing down but with the help of this website, I have been able to do lots of neat things which have saved time.

Thanks all, this site is fantastic.

PS I am thinking about getting a MAC now that I am starting to understand UNIX. Any suggestions on what I should get?

Thanks again,

MissI
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

Create one xml file from one list of names

Actually I have one list of channels names like: Rai 1 Rai 1 +1HD Rai 1 +2HD Rai 2 Rai 2 +1HD Rai 2 +2HD . . . .From this list of names I need create one new xml file with this structure <channel id="Rai 1"> <display-name lang="it">Rai 1</display-name> <icon... (3 Replies)
Discussion started by: Tapiocapioca
3 Replies

3. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

4. UNIX for Dummies Questions & Answers

Parse xml file

HI Guys, Input .XML <xn:MeContext id="L0307"> <xn:ManagedElement id="1"> <xn:VsDataContainer id="1"> <xn:attributes> <xn:vsDataType>vsDataENodeBFunction</xn:vsDataType> ... (3 Replies)
Discussion started by: pareshkp
3 Replies

5. UNIX for Dummies Questions & Answers

Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone. I need your help to create a file by using command in Linux. Files names: 0001 More... (4 Replies)
Discussion started by: ggcc
4 Replies

6. Shell Programming and Scripting

Create xml file using a content from another xml file

I need to create a xml file(master.xml) with contents from another xml files(children). I have below list of xml files in a temporary location (C:/temp/xmls) 1. child1.xml 2. child2.xml Below is the content of the child1.xml & child2.xml files, child1.xml <root> <emp> ... (3 Replies)
Discussion started by: vel4ever
3 Replies

7. Shell Programming and Scripting

parse xml file

Hello all, Given the following extract from a xml file with multiple <JOB> .... </JOB> entries <JOB APPLICATION="APP" APR="0" AUG="0" AUTHOR="AUT" AUTOARCH="0" CMDLINE="/tmp/test1 %%var" CONFIRM="1" CREATION_DATE="20100430" CREATION_TIME="130739" ... (2 Replies)
Discussion started by: cabrao
2 Replies

8. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

9. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies

10. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies
Login or Register to Ask a Question