Trev wrote:
I'm trying to use Perl to replace a line in a few XML files I have.

Example XML below, I'm wanting to change the Id= part from  Id="/Local/
App/App1" to Id=/App1". I know there's an easy way to do this with
perl alone however I'm trying to use XML::Simple or any XML plugin for
perl.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<Profile xmlns="xxxxxxxxx" name="" version="1.1" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance">


        <Application Name="App1" Id="/Local/App/App1" Services="1" policy=""
StartApp="" Bal="5" sessInt="500" WaterMark="1.0"/>


<AppProfileGuid>586e3456dt</AppProfileGuid>

</Profile>


XML::Simple may not give you what you need.

#!./perl

use warnings;
use strict;
use XML::Simple;

my $tag = <DATA>;
my $data = join "", <DATA>;

my $ref = XMLin( $data, KeepRoot => 1 );

$ref->{'Profile'}{'Application'}{'Id'} = '/Appl';

my $xml = XMLout($ref, RootName => undef ) ;

print $tag . $xml;

__DATA__
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<Profile xmlns="xxxxxxxxx" name="" version="1.1" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance">


<Application Name="App1" Id="/Local/App/App1" Services="1" policy=""
StartApp="" Bal="5" sessInt="500" WaterMark="1.0"/>


<AppProfileGuid>586e3456dt</AppProfileGuid>

</Profile>



       Output:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Profile name="" AppProfileGuid="586e3456dt" version="1.1" xmlns="xxxxxxxxx" xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance"> <Application Bal="5" Id="/Appl" Name="App1" Services="1" StartApp="" WaterMark="1.0" policy="" sessInt="500" />
  </Profile>


Note that I have to save the <?xml ... ?> tag separately, and the
<AppProfielGuid> tag becomes an attribute on output.

--
Brad

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to