You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Dubchak, John D" <Jo...@anheuser-busch.com> on 2005/12/19 19:00:58 UTC

Newbie Digester XML rules issues

Hi,

I'm having difficulty writing my XML-based digester rules to parse a simple XML
file:

<main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="myschema.xsd">

    <scenario classname="ScenarioClass">
        <child classname="ScenarioChildClass">
            <handler classname="SomeotherClass"/>
        </child>
        <child classname="AnotherScenarioChildClass"/>
        <child classname="YetAnotherScenarioChildClass"/>
    </scenario>
    
</main>

My digester XML rules are:

<object-create-rule pattern="parent" classname="java.util.ArrayList" />
<pattern value="main/scenario">
    <object-create-rule classname="Scenario"/>
    <set-properties-rule/>
    <pattern value="main/scenario/child">
        <object-create-rule classname="ChildMetaData"/>
        <set-properties-rule/>
            <pattern value="main/scenario/child/handler">
                <call-method-rule methodname="setHandler" paramcount="1"/>  
                <call-param-rule paramnumber='0' attrname='classname'/>
            </pattern>

        <set-next-rule methodname="setChildMetaData"/>
    </pattern>
    <set-next-rule methodname="add" />
</pattern>

Essentially, what I'm striving for is when the main/scenario tag is encountered,
create an instance of a Scenario class.  This occurs as it should.  

However, when it encounters main/scenario/child I would like it to create an
object of type ChildMetaData and associate the attribute of
main/scenario/child/handler specified by classname by calling the setHandler
method of ChildMetaData, then adding ChildMetaData to Scenario by calling
setChildMetaData.  Then the Scenario object gets added as an element of an
ArrayList.

I'm new to digester and have looked through the archives and the samples in the
source distro but am puzzled as to what I might be missing.

Any help is appreciated.

Thanks,
John

Re: Newbie Digester XML rules issues

Posted by Michael Nascimento <mi...@gmail.com>.
When you nest <pattern />s like this:

On 12/19/05, Dubchak, John D <Jo...@anheuser-busch.com> wrote:
> <object-create-rule pattern="parent" classname="java.util.ArrayList" />
> <pattern value="main/scenario">
>     <object-create-rule classname="Scenario"/>
>     <set-properties-rule/>
>     <pattern value="main/scenario/child">

the outer ones are prepended to the inner ones for matching. So this:

<pattern value="main/scenario/child">

is seen by Digester as:

main/scenario/main/scenario/child

Simply remove the prefix for the inner pattern elements and configure
them using relative patterns.

Regards,
Michael Nascimento Santos
https://genesis.dev.java.net/
https://thinnb.dev.java.net/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org