First your "XML" file is not really XML. That makes it hard to show you some examples of what you can do with XML::Twig. Then it is not clear from your question what you mean by "tag". Is it just the start tag, the entire element or the text of the element? The difference between tag and element is an important one in XML.

Then if all you want is extract information from the file, you might want to have a look at xml_grep, which comes with XML::Twig. Have a look at the docs. If your files are not too big (ie XML::LibXML can load them in memory), you can also use xml_grep2, by the same author (xml_grep2), which has more complete XPath support (once again, at the cost of loading the entire document in memory).

Otherwise, the code below will print the start tags of the AI elements in AC elements with a n attribute of CCC:

XML::Twig->new( twig_handlers => { 'AC[@n="CCC"]//AI' => sub { print $_->start_tag, "\n"; }->parsefile( "my.xml");

If you know that AI elements will allways be direct children of AC elements you can replace the '//' by a single '/'. If your XML file might be big, you could add a $_->purge at the end of the anonymous sub, in order to release some memory, or you could use the twig_roots option instead of twig_handlers.

I hope that gets you started.


In reply to Re: XML::Twig question by mirod
in thread XML::Twig question by thandi

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.