Create an XML tree using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create an XML tree using perl
# 8  
Old 04-29-2011
Quote:
Originally Posted by pravin27
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);

Hi,

Thanks for the quick reply.

But i have one question here if the xml node has dummy node with out any node name... some thing like this:
Code:
<Node>
<Nodename>dummy</Nodename>
<filenames>file1.txt</filenames>
<datafiles>1.csc </datafiles>

In that case the above code how will it handle and along with the attributes and the node level can be more.

Forexample for attributes:
Code:
 
student fees:code
In xml it would be
<student fees="USD">23</student fees>

How to handle this?

does sax parser helps with this?

How can i handle it?

Regards
vanitha
# 9  
Old 04-29-2011
If you are up to PERL, use a real SAX parser: PERL XML SAX Parser - Google Search

SAX is serial parsing, best for batch with potentially huge files. (The alternative, DOM, puts the entire XML file in memory as an object tree, impossible for bulk and for serial transmissions in real time.) It calls you as it traverses tags (elements), gives you direct access to attributes in and content after the start tag. It is up to you to manage state variables for where in the nesting you are.

In JAVA, I created a reusable object tree that reflected the XML syntax tree, with an abstract class to support an interface to support building classes for each type of element. PERL can probably do something similar. The PERL XML SAX lib probably has dictionary correctness checking as well, although I turned that off for speed and robustness and did my own validation.
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