Solved

perl XML::LibXML edit attribute

Posted on 2010-09-08
1
620 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
ID: 33632155
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

821 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