Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl XML::LibXML edit attribute

Posted on 2010-09-08
1
Medium Priority
?
658 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
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

971 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