 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

November 13th, 2006, 11:52 AM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
delete XML node in Perl
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
Code:
#! /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
Code:
<?xml version='1.0'?>
<staff>
<employee>
<name>
<forename>John</forename>
<surname>Doe</surname>
</name>
<age>
<dob>10-02-1967</dob>
</age>
<************>M</************>
<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>
<************>M</************>
<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>
<************>M</************>
<department>
<departmentname>Sales</departmentname>
<title>Executive</title>
</department>
<location>
<town>
<name>Kilmarnock</name>
<county>North Ayrshire</county>
</town>
</location>
</employee>
</staff>
www.crmpicco.co.uk
www.ie7.com
|

November 14th, 2006, 06:06 AM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
seemingly, something along the lines of
Code:
$xmlnode = $cgi->param('delno');
$xmldata = $xml->XMLin('employees.xml');
splice(@{$xmldata->{employee}},$xmlnode,-1);
should work, however it doesnt seem to have deleted anything
www.crmpicco.co.uk
www.ie7.com
|

November 15th, 2006, 08:08 AM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
Code:
#! /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
Code:
<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="************">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="************">M</item>
</hashref>
</item>
</arrayref>
</item>
</hashref>
</perldata>
www.crmpicco.co.uk
www.ie7.com
|

November 28th, 2006, 09:54 AM
|
Registered User
|
|
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Could you please try this code and see how it works. I thins it preserves the xml structure. I have tried to remove the second doc. from the original file.
------------------------------------------------------------------
undef $/;
$Str=(open (IN,"<$ARGV[0]"))?<IN>:die "$!";
$Str=~s/(<employee>)/<dummy>$1/g;
@count= split /<dummy>/,$Str;
undef $count[2];
$Str=join //,@count;
print $Str;
-------------------------------------------------------------------
Hope u find it useful
|

November 28th, 2006, 12:12 PM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi there, thank you for that code. i had a play around with it. this is also a working script:
Code:
#! /usr/bin/perl
# Craig R Morton
# Remove a specified node and all its child nodes from an XML document
# Last_Edit: 21-Nov-2006
use CGI qw/:standard/;
use XML::Simple;
$cgi = new CGI;
my $xml = new XML::Simple;
my $xmldoc = '';
# retrieve the passed form field value from the previous page (delno) - (which node to delete)
$xmlnode = $cgi->param('delno');
# create a reference to the original XML file 'employees.xml'
$xmldata = $xml->XMLin('employees.xml');
# remove the passed node from the XML document
splice(@{$xmldata->{employee}},$xmlnode-1, 1);
$thexml = $xmldata;
my $xmldoc = XMLout($thexml, NoAttr => 1, KeyAttr => [], RootName => "staff",);
# open 'employees.xml' and write to it with the new XML structure
open(FILE, ">employees.xml") || die "Cannot open 'employees.xml'";
print FILE "<?xml version='1.0'?>";
print FILE $xmldoc;
close(FILE) || warn "Error closing 'employees.xml'";
print "Content-type: text/html\n\n";
print <<ENDHTML;
<html>
<head>
<title>Delete Employee</title>
</head>
<body>
<br><br>
<table align='center'>
<tr>
<td>You have deleted the employee from the XML document</td>
</tr>
</table>
</body>
ENDHTML
www.crmpicco.co.uk
www.ie7.com
|
|
 |