toren has asked for the wisdom of the Perl Monks concerning the following question:

How I can change value in XML file ?

I'm a newbie to work with XML.

I'd like to change value from <Item Value="NONE"/> to <Item Value="API"/> under <Parameter Name="MapSource">

My XML file looks like this:

<Configuration> <Parameters> <Component Name="MY"> <Group Name="PrivilegesMapping"> <Parameter Name="PrivilegesLoaderInterval"> <Description> The interval (in minute) </Description> <Type>Integer</Type> <Restriction> <RequiresRestart>true</RequiresRestart> <MinVal/> <MaxVal/> <MaxLength/> <Mandatory>true</Mandatory> <Lov/> <Level>5</Level> </Restriction> <Value> <Item Value="5"/> </Value> </Parameter> </Group> <Group Name="DomainsMapping"> <Parameter Name="DomainLoaderInterval"> <Description> The interval (in minute) </Description> <Type>Integer</Type> <Restriction> <RequiresRestart>true</RequiresRestart> <MinVal/> <MaxVal/> <MaxLength/> <Mandatory>true</Mandatory> <Lov/> <Level>5</Level> </Restriction> <Value> <Item Value="5"/> </Value> </Parameter> <Parameter Name="MapSource"> <Description> Set the source of the domains list </Description> <Type>Enum</Type> <Restriction> <RequiresRestart>true</RequiresRestart> <MinVal/> <MaxVal/> <MaxLength/> <Mandatory>true</Mandatory> <Lov> <Val>FILE</Val> <Val>DATABASE</Val> <Val>NONE</Val> </Lov> <Level>5</Level> </Restriction> <Value> <Item Value="FILE"/> </Value> </Parameter> </Group> <Group Name="SystemsMapping"> <Parameter Name="MapSource"> <Description> </Description> <Type>Enum</Type> <Restriction> <RequiresRestart>true</RequiresRestart> <MinVal/> <MaxVal/> <MaxLength/> <Mandatory>true</Mandatory> <Lov> <Val>API</Val> <Val>FILE</Val> <val>NONE</Val> </Lov> <Level>5</Level> </Restriction> <Value>
<Item Value="NONE"/>
</Value> </Parameter> <Parameter Name="SystemsLoaderInterval"> <Description> The interval (in minute) </Description> <Type>Integer</Type> <Restriction> <RequiresRestart>true</RequiresRestart> <MinVal/> <MaxVal/> <MaxLength/> <Mandatory>true</Mandatory> <Lov/> <Level>5</Level> </Restriction> <Value> <Item Value="5"/> </Value> </Parameter> </Group> </Component> </Parameters> </Configuration>

Replies are listed 'Best First'.
Re: How I can change value in XML file ?
by ikegami (Patriarch) on Aug 04, 2010 at 18:05 UTC
    Using XML::LibXML,
    use strict; use warnings; use XML::LibXML qw( ); my $file_name = '...'; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file(...); my $root = $doc->documentElement(); for my $node ($root->findnodes( '//Parameter[@Name="MapSource"]' .'/Value/Item[@Value="NONE"]' )) { $node->setAttribute(Value => 'API'); } open(my $fh, '>:bytes', $file_name) or die; print($fh $doc->toString());

    Update: Forgot to select correct Parameter. Fixed.

Re: How I can change value in XML file ?
by toolic (Bishop) on Aug 04, 2010 at 16:48 UTC
    I recommend using XML::Twig. After you work through its tutorial, post the code you have tried if you can not solve your problem.
      http://xmltwig.com/ is down . It's a little bit difficult to implement CPAN specs as newbie in XML::Twig .If you have good online tutorials please replay.Thx.
        http://xmltwig.com/ is down
        Interesting. I am able to access http://xmltwig.com using an old version of Firefox on Linux. But, I can not access it from either Firefox (recent version) or IE on Windows.
        If you have good online tutorials please replay.
        That is the only tutorial that I know about. Perhaps mirod can address his website issue.
Re: How I can change value in XML file ?
by marto (Cardinal) on Aug 04, 2010 at 16:49 UTC
Re: How I can change value in XML file ?
by Your Mother (Archbishop) on Aug 04, 2010 at 18:56 UTC

    FYC: XML::LibXML has sparser docs and fewer tutorials than the very capable XML::Twig but it is a widely used lib underneath and if you learn its API, you'll be learning things that can transfer to many other places.

      Maybe I'm just "different", but in my own experience, the man pages that come with XML::LibXML are actually easier to use than the manual for XML::Twig. (Sometimes "less is more": another way of saying that the LibXML docs are "sparser" is that they are more concise).

      Apart from that, LibXML runs a lot faster, and tends to impose a much smaller memory footprint when it comes to handling a large XML structure.

        Yeah, in fact. Now I find the XML::LibXML docs quite nice but when I was starting with it way back when it was maddening. You need a decent understanding of the DOM and iterators and such to know which of the many docs to check. Once you do, they're great. Before that… uh… wha?

        Yeah, if you misuse XML::Twig and force it to keep the whole file in memory.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: How I can change value in XML file ?
by choroba (Cardinal) on Aug 05, 2010 at 10:02 UTC
    I often use XML::XSH2:
    #!/usr/bin/perl use XML::XSH2; xsh << EOX open 852913.xml ; insert attribute 'Value="API"' into //Parameter[@Name="MapSource"]// +Item[@Value="NONE"] ; save :b ; EOX