LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-09-2009, 01:38 AM   #1
madhi
Member
 
Registered: May 2008
Posts: 31

Rep: Reputation: 15
Need help in parsing XML file


Hi,

Need help in parsing xml file using shell script and generate report in a PDF file


Xml file input

<report>
<student name="x" father name="x1" class="first" Address="xyz">
<property name="sports" value="yes"/>
<property name="drawing" value="no"/>
</student>
<student name="y" father name="y1" class="first" Address="zxy">
<property name="sports" value="yes"/>
<property name="drawing" value="no"/>
</student>
<student name="z" father name="z1 class="second" Address="yzx">
<property name="sports" value="no"/>
<property name="drawing" value="yes"/>
<property name="singing" value="yes"/>
</student>
</report>

There might be n number of records

Output format (in PDF)

1. student name
x
Address
xyz
sports
Yes
drawing
No
singing
Na
2.student name
y
Address
zxy
sports
Yes
drawing
No
singing
Na
3.student name
z
Address
yzx
sports
No
drawing
Yes
singing
yes

Thanks in advance
 
Old 07-09-2009, 01:56 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
What i will suggest you is if you have access to perl use perl to parse this XML. With shell script you need to do a good amount a calculation, almost a parser in shell script
 
Old 07-09-2009, 02:13 AM   #3
madhi
Member
 
Registered: May 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks PMP for your reply,

Do we need parser stuff to be installed in perl module
can i have some points from you

Thnaks
Madhi
 
Old 07-09-2009, 02:22 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Yes, there is module called XML::Simple which can make your life much easier.

check this link http://search.cpan.org/~grantm/XML-S.../XML/Simple.pm

In case you dont have this module installed, install it on your machine along with dependecies.
 
Old 07-09-2009, 03:45 AM   #5
madhi
Member
 
Registered: May 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Hi PMP,

I have installed XML::Simple module from the link but how to i get the dependecies installed.

Thanks,
Madhi
 
Old 07-09-2009, 04:08 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
It it gets installed successfully Then all the required dependcies are already present;

Try writing a simple code as there in the example. It should work.
 
Old 07-09-2009, 04:32 AM   #7
madhi
Member
 
Registered: May 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Hi PMP,

I am running perl(5.8 version) on suse linux 10 with XML-Simple-2.18, when i run sample code i am getting error as:
not well-formed (invalid token) at line 2, column 25, byte 34 at /usr/lib/perl5/vendor_perl/5.8.8/i586-linux-thread-multi/XML/Parser.pm line 187
code:
#!/usr/bin/perl
use XML::Simple;
$xml = new XML::Simple;
$data = $xml->XMLin("data.xml");
use Data:umper;
print Dumper($data);

Thanks
Madhi
 
Old 07-09-2009, 05:15 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Seems like your XML is not valid try running xmllint on it. In case you do not have xmllint try out with simple XML. This seems a problem with the XML file.
 
Old 07-09-2009, 05:50 AM   #9
madhi
Member
 
Registered: May 2008
Posts: 31

Original Poster
Rep: Reputation: 15
xmllint soved the problem, The code is running as expected

Thanks for guiding me to write first perl program

-Madhi
 
Old 07-09-2009, 09:15 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I would guess that it doesn't like the attribute name 'father name', due to the embedded whitespace.
--- rod.
 
Old 07-10-2009, 01:23 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
If you're going to do more Perl (and you probably will), you'll need this: http://perldoc.perl.org/
 
Old 07-10-2009, 02:26 AM   #12
madhi
Member
 
Registered: May 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks chris,

I need to work on multilevel xml parsing stuff, do you have any source/links where i can get some inputs on complex parsing, right now i am using XML::LibXML module.

Thanks
Madhi
 
Old 07-10-2009, 02:36 AM   #13
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
You are parsing XML, hence you are reading XML not writing, what i will suugest you is to stick to XML::Simple it is far easier wrapper thats gives you a perfect data structure from the XML
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing xml file from a C program golden_boy615 Programming 1 03-14-2009 10:35 AM
Parsing XML file sneha hendre Linux - Newbie 2 09-15-2008 11:55 PM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 06:08 PM
awk question - parsing xml file epoo Programming 7 01-24-2007 03:13 PM
XML parsing in C irfanhab Programming 3 05-06-2006 01:47 AM


All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration