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:

  1. 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... }
  2. 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";


In reply to Re^2: XML gurus unite!! by vrk
in thread XML gurus unite!! by jmmistrot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.