Create an XML tree using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create an XML tree using perl
# 1  
Old 04-27-2011
Create an XML tree using perl

Hi,

I am having an xml file which looks like this:

Code:
<Nodes>
<Node>
	<Nodename>Student</Nodename>
	<Filename>1.txt</filename>
<Node>
	<Nodename>Dummy</Nodename>
	<Filename>22.txt</filename>
</Node>
</Node>
</Nodes>

The text files will have data like this:
#1.txt
Studentid,studentname,mothertongue:language
|||ly 2.txt ----

The output will look this
Code:
<Nodes>
<Student>
<Studentid>123</Studentid>
<studentname>name1 </studentname>
<mothertongue>english (or japan) </mothertongue>

the text file data has to be taken for the appropriate node and then xml tree has to be built...

There are very huge xml files.

How can i create an xml tree which consumes less time and with better performance?

which parser do i need to use?


Help is very much required.
Regards
Vanitha
# 2  
Old 04-27-2011
I like JAVA for XML parse, but writing it is trivial in any language. However, for well structured input files (one source), you can usually parse with sed or awk, or even shell.

You have one node inside the prior one. Is this typical, or a typo?

You do not show a source for any of the output values?
# 3  
Old 04-27-2011
Neither your example of a text file or the output document make any sense. Please provide correct examples of both.
# 4  
Old 04-28-2011
Quote:
Originally Posted by fpmurphy
Neither your example of a text file or the output document make any sense. Please provide correct examples of both.
Here is the sample text file.

Code:
#student. txt
Student id,student name, student language,student address
#student.csv
122,Ashwini,English,Bangalore
123,Amith,Kannada,Hubli
.....
....

The xml file will be like this:
Code:
<Nodes>
<Node>
<Nodename>Student Details</Nodename>
<Filename>student.txt</Filename>
<DataFile>student.csv</DataFile>
</Node>
</Nodes>

The xml tree should be constructed by taking the corresponding file name , data file of that node.

Here for example: Node name Student Details file is: student. txt, data file is: student.csv

The output should be like this:

Code:
<Student Details>
<Student id>122</Student id>
<student name>Ashwini</Student name>
 <student language> english </student language>
 <student address> bangalore </student address>
</Student Details>

Any idea?

Last edited by fpmurphy; 04-28-2011 at 09:55 AM.. Reason: fixed code tag
# 5  
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..
# 6  
Old 04-29-2011
Quote:
Originally Posted by palanisvr
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>

Hi,

Thank u very much.

But if the code is in perl it was very much appreciated.


How can i handle in perl ?
# 7  
Old 04-29-2011
Perl code,
Code:
#!/usr/bin/perl
open(FH,"<","student.xml") or die "Failure- $!\n";
while(<FH>) {
chomp;
if(/<Nodename>(.+?)<\/Nodename>/) {$nodename=$1;}
if(/<Filename>(.+?)<\/Filename>/) {$filename=$1;}
if(/<DataFile>(.+?)<\/DataFile>/) {$datafile=$1;}
}
close(FH);
open(ST,"<",$filename) or die "Failure- $!\n";
open(DT,"<",$datafile) or die "Failure- $!\n";

$file=<ST>;
chomp($file);
@flds=split(",",$file);

while(<DT>) {
chomp;
print "<",$nodename,">\n";
@data=split(",");
for($i=0;$i<=$#data;$i++) {
print "<",$flds[$i],">",$data[$i],"</",$flds[$i],">\n";
}
print "</",$nodename,">\n";
}
close(ST);
close(DT);

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
Login or Register to Ask a Question