Click to See Complete Forum and Search --> : delete xml node in Perl


crmpicco
11-13-2006, 09:43 AM
These are my files, xmlremove.pl and employees.xml, is there any way to take the value of $xmlnode (i.e. the node number of employee)
and delete that node from the document using Perl?

xmlremove.pl

#! /usr/bin/perl

use CGI;
use XML::Simple;

$cgi = new CGI;
$xml = new XML::Simple;

$xmlnode = $cgi->param('delno');
$xmldata = $xml->XMLin('employees.xml');

print "Content-type: text/html\n\n";
print "XML Node: $xmlnode<br>";


employees.xml

<?xml version='1.0'?>
<staff>
<employee>
<name>
<forename>John</forename>
<surname>Doe</surname>
</name>
<age>
<dob>10-02-1967</dob>
</age>
<sex>M</sex>
<department>
<departmentname>Operations</departmentname>
<title>Manager</title>
</department>
<location>
<town>
<name>Auchinleck</name>
<county>East Ayrshire</county>
</town>
</location>
</employee>
<employee>
<name>
<forename>Craig R.</forename>
<surname>Morton</surname>
</name>
<age>
<dob>05-03-1984</dob>
</age>
<sex>M</sex>
<department>
<departmentname>Internet</departmentname>
<title>Developer</title>
</department>
<location>
<town>
<name>Ayr</name>
<county>South Ayrshire</county>
</town>
</location>
</employee>
<employee>
<name>
<forename>William</forename>
<surname>McCann</surname>
</name>
<age>
<dob>15-07-1982</dob>
</age>
<sex>M</sex>
<department>
<departmentname>Sales</departmentname>
<title>Executive</title>
</department>
<location>
<town>
<name>Kilmarnock</name>
<county>North Ayrshire</county>
</town>
</location>
</employee>
</staff>

crmpicco
11-15-2006, 06:11 AM
Thanks.
When i run this code, it produces this output. Is there any way of maintaining the original XML structure and node names?
xmlremove.pl

#! /usr/bin/perl

use CGI;
use XML::Simple;
use XML::Dumper;
use Data::Dumper;

$cgi = new CGI;
$xml = new XML::Simple;

my $dump = new XML::Dumper;

my $perl = '';
my $xmldoc = '';

$xmlnode = $cgi->param('delno');
$xmldata = $xml->XMLin('employees.xml');

$xmlval = $xmlnode-1;

splice(@{$xmldata->{employee}},$xmlval, 1);

$perl = $xmldata;

$file = "employees.xml";
$xmldoc = $dump->pl2xml($perl,$file);

$dump->pl2xml($perl,$file);


employees.xml

<perldata>
<hashref memory_address="0xa7ab680">
<item key="employee">
<arrayref memory_address="0xa7dc1a8">
<item key="0">
<hashref memory_address="0xa7ab584">
<item key="age">
<hashref memory_address="0xa7ab338">
<item key="dob">10-02-1967</item>
</hashref>
</item>
<item key="department">
<hashref memory_address="0xa7ab1d0">
<item key="departmentname">Operations</item>
<item key="title">Manager</item>
</hashref>
</item>
<item key="location">
<hashref memory_address="0xa7ab068">
<item key="town">
<hashref memory_address="0xa7aaffc">
<item key="county">East Ayrshire</item>
<item key="name">Auchinleck</item>
</hashref>
</item>
</hashref>
</item>
<item key="name">
<hashref memory_address="0xa7ab518">
<item key="forename">John</item>
<item key="surname">Doe</item>
</hashref>
</item>
<item key="sex">M</item>
</hashref>
</item>
<item key="1">
<hashref memory_address="0xa7aae64">
<item key="age">
<hashref memory_address="0xa7aac90">
<item key="dob">05-03-1984</item>
</hashref>
</item>
<item key="department">
<hashref memory_address="0xa78c490">
<item key="departmentname">Internet</item>
<item key="title">Developer</item>
</hashref>
</item>
<item key="location">
<hashref memory_address="0xa798da4">
<item key="town">
<hashref memory_address="0xa798d38">
<item key="county">South Ayrshire</item>
<item key="name">Ayr</item>
</hashref>
</item>
</hashref>
</item>
<item key="name">
<hashref memory_address="0xa7aadf8">
<item key="forename">Craig R.</item>
<item key="surname">Morton</item>
</hashref>
</item>
<item key="sex">M</item>
</hashref>
</item>
</arrayref>
</item>
</hashref>
</perldata>