in reply to Re: XML gurus unite!!
in thread XML gurus unite!!
I agree, XML::Twig is great, although its API is humongous. It takes a while to find out what the correct name of the method is that you need, and they are not even sorted in any sensible way in the documentation.
XML::Twig also adheres to TIMTOWTDI. I can see two immediate ways to achieve your goal:
Read in the whole XML file with
XML::Twig->new()->parsefile('my xml file');then access individual nodes and change their values. (For example,
my $twig = XML::Twig->new()->parsefile('my xml file'); # Use XPath-like expressions to find the nodes you want. my @nodes = $twig->get_xpath('attribclass/attribdef[@name="mtl_Kdiff') +; for my $node (@nodes) { # Process... # Example: $node->set_att('synthetic', 'false'); } # Or navigate through references. my @attribdefs = $twig->root->first_child('attribclass')->children('at +tribdef'); for my $node (@attribdefs) { # Process... }
Create an XML filter:
my $twig = XML::Twig->new( twig_handlers => { 'attribdef' => sub { my ($twig, $elt) = @_; if ($elt->att('name') eq 'mtl_param') { # Do something. } elsif (...) { } # etc. $twig->flush; } } )->parsefile('my xml file'); $twig->flush;
This will read in the file, parse it, and while parsing, call the twig handlers defined above. The handlers can do their stuff (change element names, change values, cut and paste subtrees, and all other cool things), and the final XML text will be output to STDOUT ($twig->flush;).
However, this approach is not very practical if you first need to read in some data from the file, do some processing with them, and only later go and update the file. You could first read in the file in as a data structure (almost?) identical to what XML::Simple produces:
my $hash = XML::Twig->new()->parsefile('my xml file')->simplify(); use Data::Dumper; print Dumper($hash);
You can then do what you like with the read values, and later use that information to construct an XML filter that will produce the final file.
This sounds more complicated than it really is. On the other hand, XML is often too complicated for its own good.
Warning: code examples not tested.
--
print "Just Another Perl Adept\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: XML gurus unite!!
by Mark_UpLanguage (Sexton) on Mar 05, 2007 at 15:00 UTC |