Click here to get back home

XML file parser

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
XML file parser Bryan 04-04-2007
Posted by Bob Bessares on April 12, 2007, 5:15 am
Please log in for more thread options


> What is the best module to use for reading in an XML file, modifying a
> field and then writing it back to file? There seem to be a lot of XML
> parsers out there....
>
> Thanks,
> B

it may be helpful to get some more specifics but I have used XML::DOM
for editing parts of xml files. The syntax is nice and intuitive e.g.
replaceChild, removeChild, appendChild, addText etc...
http://search.cpan.org/dist/XML-DOM/lib/XML/DOM.pm

Here's an eg using it that alters, deletes and adds a new node to a
document:
-----------------------------------
use XML::DOM;

my $xml =<<EOXML;
<?xml version="1.0"?>
<IDList>
<Details><id>Test ID</id>
<name>Bob</name></Details>
<Details><id>Test ID</id>
<name>Brent</name></Details>
<Details><id>Test ID</id>
<name>Bren</name></Details>
<Details><id>Test ID</id>
<name>Butch</name></Details>
<Details><id>Test ID</id>
<name>Ben</name></Details>
</IDList>
EOXML

my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($xml);

my $root = $doc->getDocumentElement();

foreach my $detail (@)
{
my $name_el = $detail->getElementsByTagName('name')->[0];
my $name = $name_el->getFirstChild();
if ($name->toString() =~ /Bob/)
{
my $new = $doc->createTextNode('Foo');
$name_el->replaceChild($new, $name);
}
elsif ( $name->toString() =~ /Ben/ )
{
$root->removeChild($detail);
}
}

my $newdet = $doc->createElement('Details');
my $new_id = $doc->createElement('id');
my $new_id_val = $doc->createTextNode('New ID');
$new_id->appendChild($new_id_val);
$newdet->appendChild($new_id);
my $new_name = $doc->createElement('name');
my $new_name_val = $doc->createTextNode('Yargle');
$new_name->appendChild($new_name_val);
$newdet->appendChild($new_name);
$root->appendChild($newdet);
print $doc->toString();
------------------------------------


Similar ThreadsPosted
A c program which printing the tag value of a xml file using expat parser in linux environment October 25, 2007, 7:05 am
install HTML::Template - Problem reading cache file / Bad file number July 24, 2004, 7:55 pm
Reading contents of an excel file from a test file May 15, 2007, 2:49 am
A do-file location: how the code inside that do-file find it? January 20, 2008, 12:32 am
Need Help with XML::Parser July 5, 2005, 12:53 pm
X500::DN::Parser V 1.16 July 29, 2004, 7:20 am
Can not Install XML::Parser September 29, 2004, 5:26 pm
How to use SGML::Parser? August 2, 2005, 5:18 pm
Possible bug in HTML::Parser November 15, 2005, 5:05 pm
Installing XML Parser May 25, 2006, 2:20 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap