Solved

perl XML::LibXML edit attribute

Posted on 2010-09-08
1
608 Views
Last Modified: 2012-05-10
I am trying to open an XML file, change the value of a single attribute and then save it as another file. I want to use LibXml as this is a standard we use.
I have tried the following and many other combinations but cannot get it to work.

Can anyone help?
my $parser = XML::LibXML->new();

          my $tree = $parser->parse_file($file);

          my $root = $tree->getDocumentElement;

          my $meet_date = $root->getAttribute('meetingdate');

         



          my($node)  = $root->find('@meetingdate');

         $node->setData($meetingdate);



          print $tree->toString;





========== XML file stub ========

<?xml version="1.0" ?> 

- <meeting meetingdate="08 Sep 2010" meetingJetbetnumber="9" meetingType="GR"

Open in new window

0
Comment
Question by:jetbet
1 Comment
 

Accepted Solution

by:
jetbet earned 0 total points
Comment Utility
I have found the answer myself. Please close this question
foreach my $file (@input_files)

    {

        $input_file = basename($file);

        if ($input_file =~ /(Ver4_)(\d{4}_\d{2}_\d{2})(.*)/)

        {

          $prefix = $1;

          $filedate = $2;

          $suffix = $3;

          my $today = strftime("%Y_%m_%d", localtime);

          $newfilename = $1.$today.$3;

          print ("from $input_file to $newfilename \n");



          $meetingdate = strftime("%d %b %Y", localtime);



          my $parser = XML::LibXML->new();

          my $tree = $parser->parse_file($file);

          my $root = $tree->getDocumentElement;



          $root->setAttribute( 'meetingdate', $meetingdate );



          $tree->toFile('processed/'.$newfilename);



        }

    }

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now