By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

How to modify XML data with Perl

P: 1
Hi.

Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements that I want to modify. But all of the code examples that I have seen assume that I want to assign the targeted elements to variables, modify each element, and then print only the modified elements out to a file. For example, this code is found at http://search.cpan.org/~mirod/XML-DOM-XPath-0.13/XPath.pm#findnodes($path)

use XML::DOM::XPath;

my $parser= XML::DOM::Parser->new();
my $doc = $parser->parsefile ("file.xml");

# print all HREF attributes of all CODEBASE elements
# compare with the XML::DOM version to see how much easier it is to use
my @nodes = $doc->findnodes( '//CODEBASE[@HREF]/@HREF');
print $_->getValue, "\n" foreach (@nodes);

Using the example above, how can I modify the targeted elements WITHIN $doc ITSELF and then print out the entire document, instead of assign the targeted elements to @nodes and print out only the elements in @nodes?

Any clues would be appreciated.

Novice Perl programmer,

Tim
Jun 27 '07 #1
Share this Question
Share on Google+
1 Reply


Dököll
Expert 100+
P: 2,364
Hi.

Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements that I want to modify. But all of the code examples that I have seen assume that I want to assign the targeted elements to variables, modify each element, and then print only the modified elements out to a file. For example, this code is found at http://search.cpan.org/~mirod/XML-DOM-XPath-0.13/XPath.pm#findnodes($path)
Expand|Select|Wrap|Line Numbers
  1. use XML::DOM::XPath;
  2.  
  3.   my $parser= XML::DOM::Parser->new();
  4.   my $doc = $parser->parsefile ("file.xml");
  5.  
  6.   # print all HREF attributes of all CODEBASE elements
  7.   # compare with the XML::DOM version to see how much easier it is to use
  8.   my @nodes = $doc->findnodes( '//CODEBASE[@HREF]/@HREF');
  9.   print $_->getValue, "\n" foreach (@nodes);
  10.  
Using the example above, how can I modify the targeted elements WITHIN $doc ITSELF and then print out the entire document, instead of assign the targeted elements to @nodes and print out only the elements in @nodes?

Any clues would be appreciated.

Novice Perl programmer,

Tim
This looks fetching, Tim, additional info from same site. Please let us know if you'd already seen it:

http://search.cpan.org/~boewe/XML-YYLex-0.04/YYLex.pm
Jun 28 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.