Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 13th, 2006, 11:52 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default 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
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote
  #2 (permalink)  
Old November 14th, 2006, 06:06 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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
Reply With Quote
  #3 (permalink)  
Old November 15th, 2006, 08:08 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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
Reply With Quote
  #4 (permalink)  
Old November 28th, 2006, 09:54 AM
Registered User
 
Join Date: Nov 2006
Location: , , India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old November 28th, 2006, 12:12 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete a xml node with all attributes and s vishnu108mishra XML 2 November 14th, 2007 04:19 AM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM
finished javascript function to delete XML node crmpicco Javascript 1 April 20th, 2006 11:19 AM
delete XML node with javascript crmpicco Javascript How-To 5 April 6th, 2006 03:35 AM
how to add or delete a node from an xml documnet sai XML 1 March 22nd, 2004 06:54 AM



All times are GMT -4. The time now is 04:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.