Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

perl XML::LibXML edit attribute

Posted on 2010-09-08
1
Medium Priority
?
650 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

722 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