bobf has asked for the wisdom of the Perl Monks concerning the following question:
I am still trying to wrap my head around XML::Twig and I could really use a nudge in the right direction. The docs just aren't making it through the fog and into my head today.
I am trying to parse an XMI file that has the following overall structure (greatly simplified, of course):
<XMI xmlns:UML="omg.org/UML1.3" xmi.version="1.1" timestamp="2007- +06-26..."> <XMI.content> <UML:Class name="Address" xmi.id="EAID_009DB5F9_3A9D_44d0_ +88BD_..."> <UML:ModelElement.taggedValue> <UML:TaggedValue tag="isSpecification" value="false" / +> <UML:TaggedValue tag="ea_stype" value="Class" /> </UML:ModelElement.taggedValue> <UML:Classifier.feature> <UML:Attribute name="city" changeable="none" > <UML:ModelElement.taggedValue> <UML:TaggedValue tag="description" value="City" /> <UML:TaggedValue tag="ea_guid" xmi.id="EAID_661614 +4D_..." /> </UML:ModelElement.taggedValue> </UML:Attribute> </UML:Classifier.feature> </UML:Class> <UML:TaggedValue tag="created" xmi.id="EAID_17DA671B_9257_42f1_8AA8_0EF5F305DBD0" modelElement="EAID_31EB028E_30B2_430a_9168_1010A2A7B851" / +> <UML:TaggedValue tag="ea_stype" xmi.id="EAID_FAF2F308_9EB4_4f83_90F6_BE8ABC10087C" modelElement="EAID_31EB028E_30B2_430a_9168_1010A2A7B851" / +> </XMI.content> </XMI>
The handlers I created are as follows:
my $twig = XML::Twig->new( start_tag_handlers => { 'UML:Class' => \¨_class_start, 'UML:Package' => \¨_package_start, }, twig_handlers => { # purge at the end of each class section 'UML:Class' => sub { $_[0]->purge }, 'UML:Attribute' => \¨_attribute, 'UML:Generalization' => \¨_generalization, 'UML:TaggedValue' => \¨_taggedvalue, }, );
I need to get the value of different tags, but I suspect that I need different handlers for each, depending on the context. For example,
I don't need any of the UML:TaggedValue tags.my $xmi_id = $elt->att( 'xmi.id' ); my $name = $elt->att( 'name' );
How can I tell XML::Twig to ignore UML::TaggedValue tags in Classes, handle them one way in Attributes, and handle them a different way if they are not in either Classes or Attributes? (There are a couple of additional cases, too, but I can solve those by generalizing the solution to this question.)
Thanks very much, in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating context-specific handlers in XML::Twig
by grinder (Bishop) on Nov 08, 2007 at 23:52 UTC | |
by bobf (Monsignor) on Nov 09, 2007 at 05:17 UTC | |
|
Re: Creating context-specific handlers in XML::Twig
by mirod (Canon) on Nov 09, 2007 at 09:52 UTC | |
by bobf (Monsignor) on Nov 11, 2007 at 05:35 UTC | |
|
Re: Creating context-specific handlers in XML::Twig
by GrandFather (Saint) on Nov 09, 2007 at 07:03 UTC |