in reply to Add New Attribute to XML

I've a question, why pick XML::XPath, it hasn't been updated in years?

I'm a fan of XML::Twig, so I would adapt this example

#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $source = '/sand/devel/sthapa/tmp/as.xcf-dist'; my $Tag_Resource ='/CONFIGURATION/APPLICATION_SERVER/RESOURCE_LIST/'; my $Tag_Resource_PI = $Tag_Resource.'PLATFORM_INDEPENDENT/RESOURCE'; my $Tag_wnt = $Tag_Resource.'WNT/RESOURCE'; my $t = XML::Twig->new( twig_roots => { # /CONFIGURATION/APPLICATION_SERVER/RESOURCE_LIST/WNT/RESOURCE $Tag_wnt => sub { my ( $t, $price ) = @_; { my $id = $price->att('Id'); if (exists $$HRR_resource{$id}){ print "Attribute exists: $id"; } } $price->print(\*STDOUT); }, }, twig_print_outside_roots => \*STDOUT, ); $t->parsefile($source); $t->flush; #don't forget undef $t;
or try adapting XML::LibXML example

Replies are listed 'Best First'.
Re^2: Add New Attribute to XML
by Sun751 (Beadle) on Jun 22, 2009 at 07:32 UTC
    I am wondering if there is any other way to do this either by use XML::XPath or XML::XPath::Xparser???

      Looking at the Source of XML::XPath::Node, there is the insertAfter method for nodes, which you can use to append other nodes after an existing node. But it doesn't seem like XML::XPath is geared towards XML manipulation. You're likely better off using one of the other XML modules. Potentially, XML::LibXML is geared towards both, XPath and DOM manipulation.

        Can you suggest me some basic tutorial about XML::LibXML!!! Cheers!
        Could any one suggest me how can i use XML::LibXML to achieve my target? as in above code i am trying to check if the attribute exists in hash, if not add new attribute!!! any suggestion!!! Cheers
      Probably, but I can't help you with that, I don't use either :)