XML file parser
am 05.04.2007 02:33:24 von BryanWhat 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
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
> 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
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/
On Apr 4, 5:33 pm, Bryan
> 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
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($xml);
my $root = $doc->getDocumentElement();
foreach my $detail (@{$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();
------------------------------------
Post removed (X-No-Archive: yes)
Tom
> 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