XML file parser

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
Bryan [ Do, 05 April 2007 02:33 ] [ ID #1679998 ]

Re: XML file parser

Bryan <bcc [at] nospam.net> writes:

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

That's far too vague to give a useful answer.

Best in what way? Memory use? Speed? Ease of coding? Portability? Do you
want an event-driven parser, a DOM parser, or one that gives you native
Perl data structures like hashes and arrays?

The reason there are so many parsers is that each has strengths and weak-
nesses. There isn't just one "best" module - you have to choose the one
whose strengths are a good fit for the job you're doing.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sherm Pendley [ Do, 05 April 2007 03:08 ] [ ID #1680000 ]

Re: XML file parser

Bryan wrote:

Hi Bryan

> What is the best module to use for reading in an XML file, modifying a

http://perl-xml.sourceforge.net/faq/
Ron Savage [ Sa, 07 April 2007 02:50 ] [ ID #1681396 ]

Re: XML file parser

On Apr 4, 5:33 pm, Bryan <b... [at] nospam.net> wrote:
> 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 ( [at] {$root->getChildNodes()})
{
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();
------------------------------------
Bob Bessares [ Do, 12 April 2007 11:15 ] [ ID #1686228 ]

Re: XML file parser

Post removed (X-No-Archive: yes)
Notifier Deamon [ Fr, 20 April 2007 19:20 ] [ ID #1693997 ]

Re: XML file parser

Tom <tom [at] to.com> writes:

> Taking a bit of a crash course in XML/SOAP. I got as far as getting a response
> back with XML code but having a tough time parsing it.
>
> Not sure if it's unique to me, but I'm getting a lot of data back as one line.
> Is that normal and would that mean I should probably take the "tree" style
> approach with XML::Parser?

The "normal" approach is to use Soap::Lite or some other module to send the
request and parse the response. You don't need to parse XML just to handle a
simple SOAP request. Why reinvent the wheel?

Anyway, to answer your question - callback-based XML parsers don't call your
handler functions for lines of text, they call them for tags. So it doesn't
matter if the XML is one line or a thousand.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sherm Pendley [ Fr, 20 April 2007 21:05 ] [ ID #1693998 ]
Perl » comp.lang.perl.modules » XML file parser

Vorheriges Thema: Perl on Windows
Nächstes Thema: Issues with Proc::ProcessTable module on Solaris 9