Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: Choosing the best XML module for a toy system

by ForgotPasswordAgain (Deacon)
on Sep 12, 2008 at 11:15 UTC ( #710884=note: print w/replies, xml ) Need Help??

in reply to Re: Choosing the best XML module for a toy system
in thread Choosing the best XML module for a toy system

I used to go with XML::Twig, but for some reason I started to get annoyed with it. I guess it's a matter of taste. I always use XML::LibXML anymore. It's a bit harder, maybe, to get started with, though. For example, if you look in the SYNOPSIS of its docs, it's difficult to know how to use it. You have to know its classes are (nicely) broken down mostly like the XML DOM, so you sometimes have to search for docs.

Here's maybe a quickstart guide, at least how I use it (I usually encapsulate parts of this in functions):

use XML::LibXML qw(:all); my $parser = XML::LibXML->new(); # this could also be parse_string my $xmldom = eval { $parser->parse_file($file) }; if ($@) { # problem parsing, die $@, etc. } # this is the outermost element my $doc = $xmldom->documentElement; # the rest might be familiar if you've used # the DOM in JavaScript # findnodes is very useful, but hard to find # (look in XML::LibXML::Node); this assumes there's # XML like <asset><story>...</story><story>...</story></asset> my $xpath = 'asset/story'; # this can be whatever XPath my @story_nodes = $doc->findnodes($xpath); foreach my $story_node (@story_nodes) { my $id = $story_node->getAttribute('ID'); # this is an example where DOM can be annoying my ($uri_node) = $story_node->getChildrenByTagName('URI'); $uri_node->normalize(); my $uri = $uri_node->firstChild->getData(); # should 1st check i +t's ::Text ! $uri =~ s/^(http://)[^.]+(\.example\.)/$1new$2/; $uri_node->setData($uri; # .... } # another thing not so obvious, this is in ::Document # there are several variations, toFH, toString, etc.. $doc->toFile($newfile);

Replies are listed 'Best First'.
Re^3: Choosing the best XML module for a toy system
by Your Mother (Bishop) on Sep 12, 2008 at 21:50 UTC

    Sure. Part of what I like about XML::LibXML is that my JavaScript chops improved as a side-effect of working with it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710884]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2018-04-27 05:37 GMT
Find Nodes?
    Voting Booth?