Sponsored Content
Top Forums Shell Programming and Scripting Create an XML tree using perl Post 302517943 by palanisvr on Thursday 28th of April 2011 08:58:58 AM
Old 04-28-2011
Try this ......

Sample file 1 :
Code:
$cat student.txt
Student id,student name, student language,student address

Sample file 2 :
Code:
$ cat student.csv
122,Ashwini,English,Bangalore
123,Amith,Kannada,Hubli

Script :
Code:
#!/bin/ksh

cat xml_file|grep "Nodename"  | sed  -e s/\<Nodename\>// |sed  s/\<\\/Nodename\>// > main_tag.txt
cat xml_file|grep "Filename"  | sed  -e s/\<Filename\>// |sed  s/\<\\/Filename\>// > file_name.txt
cat xml_file|grep "DataFile"  | sed  -e s/\<DataFile\>// |sed  s/\<\\/DataFile\>// > data_file.txt

main_tag=`cat main_tag.txt`
file_name=`cat file_name.txt`
data_file=`cat data_file.txt`



if [ -f xml.txt ] 
then
rm xml.txt
fi 

for i in `cat $data_file`
do


col_std_id=`cat $file_name | awk -F ',' '{print $1'}`
col_std_nm=`cat $file_name  | awk -F ',' '{print $2'}`
col_std_lang=`cat $file_name  | awk -F ',' '{print $3'}`
col_std_add=` cat $file_name  | awk -F ',' '{print $4'}`


val_std_id=`echo $i | awk -F ',' '{print $1'}`
val_std_nm=`echo $i | awk -F ',' '{print $2'}`
val_std_lang=`echo $i | awk -F ',' '{print $3'}`
val_std_add=`echo $i | awk -F ',' '{print $4'}`



echo "<$main_tag>" >> xml.txt
echo "<$col_std_id>$val_std_id</$col_std_id>" >>xml.txt
echo "<$col_std_nm>$val_std_nm</$col_std_nm>" >>xml.txt
echo "<$col_std_lang>$val_std_lang</$col_std_lang>" >>xml.txt
echo "<$col_std_add>$val_std_add</$col_std_add> ">>xml.txt
echo "</$main_tag>" >> xml.txt


done

echo "final output of the file "
cat  xml.txt


Output :
Code:
$ cat xml.txt
<Student Details>
<Student id>122</Student id>
<student name>Ashwini</student name>
< student language>English</ student language>
<student address>Bangalore</student address>
</Student Details>
<Student Details>
<Student id>123</Student id>
<student name>Amith</student name>
< student language>Kannada</ student language>
<student address>Hubli</student address>
</Student Details>


Last edited by palanisvr; 04-28-2011 at 10:05 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a binary tree

I need to create a binary tree like structure of directories using shell script... does anyone know of any algorithm for this ? i tried doing a recursive algorithm function CreateDir { level=$1 dirname=$2 mkdir $dirname/sub1/ mkdir $dirname/sub2/ let level=level-1 ... (2 Replies)
Discussion started by: macvijay1985
2 Replies

2. UNIX for Dummies Questions & Answers

Commands to create hierarchical tree structure

I am creating a hierarchical tree structure and I was wondering what commands I needed to do that. I have 4 directories and sixteen sub directories and 4 files. Thank you for your help in getting my started in right direction.:confused: (1 Reply)
Discussion started by: GreginNC
1 Replies

3. Shell Programming and Scripting

How to create a xml file using Perl Script

Hi All, I have some data which needs to be saved in the xml file format.Can you guys please let me know how to do this using perl script. NOTE: the template of the xml file shall be depending on validation of the data done for some requirements. Basically to summarise, the fields in the xml... (5 Replies)
Discussion started by: girish.raos
5 Replies

4. UNIX for Dummies Questions & Answers

How to create this tree?

a buddy and i are trying to re-learn basic commands. i havent used linux for awhile. so i need help on this. what are the commands to create a tree like this. . |-- a1.A |-- a1.B |-- opt | |-- documents | | `-- tmp | | |-- backup | | `-- etc | |-- music | `--... (1 Reply)
Discussion started by: ink
1 Replies

5. Programming

extract xml data and create word document using perl.

hi, i have large xml file which contains students information, i need to extract student number and some address tags and create a word document for the extracted data. my data looking llike this <student> <number>24</number> <education>bachelors</education> ... (1 Reply)
Discussion started by: veerubiji
1 Replies

6. Programming

Extract xml data and create word document using perl.

Hi, I have large xml data file.I need to extract node and some tags in the node and after I need to create word document. my XMl data is look like as below -<student> <number>24</number> <education>bachelor</bachelor> <specialization>computers</specialization> ... (3 Replies)
Discussion started by: veerubiji
3 Replies

7. Programming

help need in the perl script that create one xml file form multiple files.

Hi every one, Please excuse me if any grammatical mistakes is there. I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this</p> file1:bvr.xml ... (0 Replies)
Discussion started by: veerubiji
0 Replies

8. 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

9. Shell Programming and Scripting

Need to read a csv and create xml - Perl

I have a csv file like below. john,r2,testdomain1,john.r2@hex.com,DOMAINADMIN,testdomain1.dom maxwell,b2, testdomain1,maxwell.b2@hex.com,DOMAINADMIN,testdomain1.dom I would need the perl script to read the above csv and create an xml like below. <Users> ... (1 Reply)
Discussion started by: Tuxidow
1 Replies
XML_PP(1p)						User Contributed Perl Documentation						XML_PP(1p)

NAME
xml_pp - xml pretty-printer SYNOPSYS
xml_pp [options] [<files>] DESCRIPTION
XML pretty printer using XML::Twig OPTIONS
-i[<extension>] edits the file(s) in place, if an extension is provided (no space between "-i" and the extension) then the original file is backed-up with that extension The rules for the extension are the same as Perl's (see perldoc perlrun): if the extension includes no "*" then it is appended to the original file name, If the extension does contain one or more "*" characters, then each "*" is replaced with the current filename. -s <style> the style to use for pretty printing: none, nsgmls, nice, indented, record, or record_c (see XML::Twig docs for the exact description of those styles), 'indented' by default -p <tag(s)> preserves white spaces in tags. You can use several "-p" options or quote the tags if you need more than one -e <encoding> use XML::Twig output_encoding (based on Text::Iconv or Unicode::Map8 and Unicode::String) to set the output encoding. By default the original encoding is preserved. If this option is used the XML declaration is updated (and created if there was none). Make sure that the encoding is supported by the parser you use if you want to be able to process the pretty_printed file (XML::Parser does not support 'latin1' for example, you have to use 'iso-8859-1') -l loads the documents in memory instead of outputing them as they are being parsed. This prevents a bug (see BUGS) but uses more memory -f <file> read the list of files to process from <file>, one per line -v verbose (list the current file being processed) -- stop argument processing (to process files that start with -) -h display help EXAMPLES
xml_pp foo.xml > foo_pp.xml # pretty print foo.xml xml_pp < foo.xml > foo_pp.xml # pretty print from standard input xml_pp -v -i.bak *.xml # pretty print .xml files, with backups xml_pp -v -i'orig_*' *.xml # backups are named orig_<filename> xml_pp -i -p pre foo.xhtml # preserve spaces in pre tags xml_pp -i.bak -p 'pre code' foo.xml # preserve spaces in pre and code tags xml_pp -i.bak -p pre -p code foo.xml # same xml_pp -i -s record mydb_export.xml # pretty print using the record style xml_pp -e utf8 -i foo.xml # output will be in utf8 xml_pp -e iso-8859-1 -i foo.xml # output will be in iso-8859-1 xml_pp -v -i.bak -f lof # pretty print in place files from lof xml_pp -- -i.xml # pretty print the -i.xml file xml_pp -l foo.xml # loads the entire file in memory # before pretty printing it xml_pp -h # display help BUGS
Elements with mixed content that start with an embedded element get an extra <elt><b>b</b>toto<b>bold</b></elt> will be output as <elt> <b>b</b>toto<b>bold</b></elt> Using the "-l" option solves this bug (but uses more memory) TODO
update XML::Twig to use Encode with perl 5.8.0 AUTHOR
Michel Rodriguez <mirod@xmltwig.com> perl v5.12.4 2011-05-18 XML_PP(1p)
All times are GMT -4. The time now is 08:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy