Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl and XML

Status
Not open for further replies.

celfyn

Technical User
Jan 4, 2008
8
GB
Hello,
I am working on a perl script which will (in the end) take an xml file, get the necessary data and then insert it into an SQL database. I am new to Perl and xml, so am having a bit of trouble :(

Here is an extracte of the xml file - the is auto generated.
Code:
<?xml version='1.0'?>
<report rpt_cp_name="All Courses" rpt_created="2007-12-11 10:29:54" rpt_createdby="admin" rpt_mode="0" rpt_custdata="" rpt_wgpCount="1">

<workgroup wgp_number="1" wgp_id="" wgp_name="All users" wgp_groups="All groups" wgp_userCount="2441" wgp_coursesStarted="2" wgp_coursesCompleted="0" wgp_avgPcntComplete="0">

<user user_id="2620" user_login="1234" user_lastname="" user_firstname="" user_email="" user_created="07 Dec 2007 10:56" ><data co_id="1" co_name="some course" co_number="10" course_started="-" cp_started="-" course_completed="-" cp_completed="-" percentCorrect="0" totalScore="-" cp_percentComplete="0" cp_status="0" cp_attempt="0"/>
</user>

<user user_id="2645" user_login="12345" user_lastname="" user_firstname="" user_email="" user_created="07 Dec 2007 10:56"><data co_id="1" co_name="some course" co_number="10" course_started="-" cp_started="-" course_completed="yes" cp_completed="-" percentCorrect="0" totalScore="-" cp_percentComplete="0" cp_status="0" cp_attempt="0"/>
</user>
...

I want to extract the attributes users_firstname, user_lastname if the user has completed the course (course_completed attribute).

I have been trying to do this using the module XPath. Here is an extract of my Perl code...
Code:
my $file= 'filename.xml';
my $open_file = XML::XPath->new(filename => $file) || die "Cannot open xml file\n";

foreach my $record ($open_file->findnodes('//report/workgroup/user'))
{
	my $user_login;
	my $fragment_login;
	my $user_last_name;
	my $user_first_name;

	foreach my $record_finished ($open_file->findnodes('//report/workgroup/user/data[@course_completed="yes"]'))
	{
		$user_login		= $record->getAttribute("user_login");
		$user_last_name		= $record->getAttribute("user_lastname");
		$user_first_name	= $record->getAttribute("user_firstname");

		print "Login: $user_login\n";
		print "First name: $user_first_name\n";
		print "Last name: $user_last_name\n";
	}
}

This gets the necessary data, but loops through and prints out every user in xml file x the number of users have passed a course. All I want it to output are the ones which have completed the course. Can anyone please help??


 
Sorry, I have figured it out.

Here is how I prcess the xml file now...

Code:
foreach my $user ($open_file->find('//user')->get_nodelist)
{
	my $user_login;
	my $fragment_login;
	my $user_last_name;
	my $user_first_name;
	my $course_completed;

	$user_login		= $user->find('@user_login');
	$user_last_name		= $user->find('@user_firstname');
	$user_first_name	= $user->find('@user_firstname');
	$course_completed	= $user->find('data/@course_completed');

    print ' (' . $user_login . ') ';
    print ' (' . $user_first_name . ') ';
    print ' (' . $user_last_name . ') ';
    print ' (' . $course_completed . ') ';
    print "\n";

}

Thanks anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top