473,508 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to modify XML data with Perl

1 New Member
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
1 7414
Dököll
2,364 Recognized Expert Top Contributor
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

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

Similar topics

4
2389
by: spar | last post by:
I'm converting a Perl script to Python and have run into something I'm not sure how to do in Python. In Perl, I am running through a couple loops and inserting values directly into a complex...
3
2644
by: Vumani Dlamini | last post by:
I have data which is in this format: ### data ### area=1101 home=003 mzer=00020 mzec=101 pmpr=00000 pmpc=102 bnsr=00000 bnsc=103
0
5280
by: Eric | last post by:
I've got a weird problem, regardless of how often I enter: perl -MCPAN -e 'install "Data::Dumper"' I never get a message telling me that it is up-to-date. It will always try to reinstall even...
4
3223
by: Sherman Willden | last post by:
I am trying to use Perl's XML::Twig to modify a version number in an XML document. At the very end of this posting is an excerpt from the xml document. Just before the xml excerpt is the Perl code...
2
6465
by: Veeven | last post by:
Hi, Suppose I have an XML file like this on the web, say, http://www.example.com/list.xml <?xml version='1.0'?> <list> <item id='itm01'> <name>Item 1</name> <desc>This is item 1</desc>
2
1942
by: Elby | last post by:
I'm looking for a the most simple and generic way to modify a file, with the possibility of making backups. In fact, I would like to emulate Perl's -i option. here is a bit of code, to explain...
23
2483
by: smarsh | last post by:
Hi, Someone put together a small perl script for me that uses day and time to determine which of two images to display (open.gif or closed.gif). I need to modify the script to also include a...
5
6667
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
3
7302
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.