I am trying to define handlers for XML::Twig that will allow me to parse out different sections of an XMI document (created by exporting a UML model from Enterprise Architect) and process them separately, but I can't seem to grok the syntax. A very simplified version of the document looks like this:

<XMI xmlns:UML="omg.org/UML1.3" xmi.version="1.1" timestamp="2006-08-0 +4 16:29:41"> <XMI.header> <XMI.documentation> <XMI.exporter>Enterprise Architect</XMI.exporter> <XMI.exporterVersion>4.1RR</XMI.exporterVersion> </XMI.documentation> </XMI.header> <XMI.content> <UML:Model name="EA Model" xmi.id="MX_EAID_..."> <UML:Namespace.ownedElement xmi.id="MX_EAID_..."> <!-- note the UML:Class tag on the next line --> <UML:Class name="EARootClass" xmi.id="EAID_..." /> <UML:Package name="Logical View" xmi.id="EAPK..."> <UML:Namespace.ownedElement xmi.id="EAPK..."> <UML:Package name="Logical Model" xmi.id="EAPK..."> <UML:Namespace.ownedElement xmi.id="EAPK..."> <!-- many UML:Package and UML:Namespace tags removed for brevity --> <!-- I want to pull out the following UML:Class block --> <UML:Class name="DataType" xmi.id="EAID... +"> <UML:Classifier.feature xmi.id="EAID..." +> <!-- I want to pull out the following UML:Attribute blocks --> <UML:Attribute name="dataTypeId"> </UML:Attribute> <UML:Attribute name="name"> </UML:Attribute> </UML:Classifier.feature> </UML:Class> <!-- I want to pull out the following UML:Association blocks --> <UML:Association xmi.id="EAID..."> </UML:Association> <UML:Association xmi.id="EAID..."> </UML:Association> <!-- many UML::Class and UML::Association blocks removed (I want these +, too) --> <!-- miscellaneous blocks removed --> </UML:Namespace.ownedElement> </UML:Package> </UML:Namespace.ownedElement> </UML:Package> <UML:DataType xmi.id="eaxmiid3" /> <UML:DataType xmi.id="eaxmiid1" /> </UML:Namespace.ownedElement> </UML:Model> <!-- I want to process each of the UML::TaggedValue elements --> <UML:TaggedValue tag="complexity" /> <UML:TaggedValue tag="ea_stype" /> <!-- many UML::TaggedValue tags removed --> </XMI.content> <XMI.extensions xmi.extender="Enterprise Architect 2.5" /> </XMI>

I tried a few things, but the closest I came to getting what I want is the following:

use strict; use warnings; use Data::Dumper; use XML::Twig; my $twig = XML::Twig->new( twig_roots => { 'UML:Class' => \&uml_class +} ); $twig->parsefile( 'testfile.xmi' ); sub uml_class { my ( $twig, $section ) = @_; my $elt = $twig->first_elt; my $struct = $elt->simplify( forcearray => 1 ); print Dumper( $struct ); # parse the block and extract the data elements $twig->purge; }

I can't seem to figure out how to grab a UML:Class block and send it to a parser - it always contains additional root elements and/or is missing child tags. I think I can figure out how to write separate parsers for the UML::Class, UML::Attribute, UML::Association, and UML::TaggedValue tags, but I need to isolate them first.

I must be making this too difficult. Could someone clue me in, please?

Thanks in advance!


In reply to Pulling out sections of an XMI file with XML::Twig by bobf

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.