Solved

perl XML::LibXML edit attribute

Posted on 2010-09-08
1
629 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

710 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