in reply to Re^2: xml remove attribute
in thread xml remove attribute
No need to do a nested descent through the tree when you know in advance which nodes you want to alter. This revised version of your code does the trick for your sample data:
#!/usr/bin/perl use strict; use warnings; use XML::LibXML; die "Usage: $0 filename \n" unless ( @ARGV > 0 ); my $xml_file = shift; my $xml = XML::LibXML->new; my $dom = $xml->parse_file( $xml_file ); foreach my $add_user ( $dom->getElementsByTagName('ADD_USER') ) { $add_user->removeAttribute( "USER_NAME" ); $add_user->removeAttribute( "PASSWORD" ); } my $output = $dom->toString(0); $output =~ s/(?<=\n)\s*\n//g; open ( my $FH, '>', 'newilo2' ) or die "Could not open file newilo2 $! +"; print $FH $output; close $FH;
XML::LibXML is pretty powerful, but it's a steep old learning curve. If you do a lot of XML work it would pay to immerse yourself in the documentation for a while to let it all sink in. Good luck.
Edit: better choice of verb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: xml remove attribute
by BradV (Sexton) on Aug 19, 2016 at 17:06 UTC |