You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Martin Wegner <ma...@yahoo.com> on 2005/02/03 19:13:12 UTC

Default Values and XMLBeans

Is there a definitive advice as to the use of XML Schemea default values
and XMLBeans?  Is it true that the user of XMLBeans has to manually add
the default values after the parsing?  Is that the recommended practive?

Thanks.


--Marty


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


Re: Default Values and XMLBeans

Posted by Martin Wegner <ma...@yahoo.com>.
Marius,

Thanks to the code you gave I was indeed able to modify it to apply
default values to all nodes in the DOM after the parse.  It uses XPath so
the code is pretty straight forward.  I bet there is a more efficient way
to do it but it works for me.

And it should be noted that the code I added was only because I wanted the
default attributes to be physically attached to the DOM.  That way the
XMLBeans encoder will spit them out.  That was a requirement for my
project which is probably off the norm for most XMLBeans users.  The
standard default value behavior for XMLBeans is sufficient for most cases
I would imagine.


--Marty

--- Marius Gleeson <xm...@subscribe.audumla.net> wrote:

> Hi Marty,
> 
> This is only used for when I create a new bean, and so I just need to
> initialize all of its default values without worrying if there is an
> existing structure. If you want to initialize an existing dom tree then
> you would need to modify this method to check for child nodes that
> already exist and hand them to the initialize method as well as any
> newly instantiated ones. You would also need to make sure that when
> instantiating a new child node or setting a default attribute that it
> was not already set as this would then wipe over existing nodes.
> This would be a pretty small change. 
> 
> If someone wanted to look into all of the different types that could be
> defaulted and any complex stuctures that my method does not cater for,
> this could be extended to allow a generic mechanism to initialize
> XmlBeans.
> 
> Cheers,
> Marius.


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


Re: Default Values and XMLBeans

Posted by Marius Gleeson <xm...@subscribe.audumla.net>.
Hi Marty,

This is only used for when I create a new bean, and so I just need to
initialize all of its default values without worrying if there is an
existing structure. If you want to initialize an existing dom tree then
you would need to modify this method to check for child nodes that
already exist and hand them to the initialize method as well as any
newly instantiated ones. You would also need to make sure that when
instantiating a new child node or setting a default attribute that it
was not already set as this would then wipe over existing nodes.
This would be a pretty small change. 

If someone wanted to look into all of the different types that could be
defaulted and any complex stuctures that my method does not cater for,
this could be extended to allow a generic mechanism to initialize
XmlBeans.

Cheers,
Marius.



On Thu, 3 Feb 2005 20:29:07 -0800 (PST), "Martin Wegner"
<ma...@yahoo.com> said:
> Marius,
> 
> That is very useful.  Do you not bother with travesing the entire DOM? 
> Your solution does not walk all of the nodes in the XML tree.
> 
> 
> --Marty
> 
> --- Marius Gleeson <xm...@subscribe.audumla.net> wrote:
> 
> > Unless something has changed recently that is right, you have to do it
> > manually, however
> > I have the same issue and so I wrote the following to help get
> > initialized beans
> > 
> >     protected void initializeBean(XmlObject o, SchemaType st) {
> >         SchemaProperty[] sp = st.getAttributeProperties();
> >         XmlCursor c = o.newCursor();
> >         for (int i = 0; i < sp.length; ++i ) {
> >             if (sp[i].hasDefault() == SchemaProperty.CONSISTENTLY) {
> >                
> > c.setAttributeText(sp[i].getName(),sp[i].getDefaultText());
> >             }
> >         }
> >         c.dispose();
> >         sp = st.getElementProperties();
> >         for (int i = 0; i < sp.length; ++i ) {
> >             SchemaProperty se = sp[i];
> >             try {
> >                 Method m =
> >                
> > o.getClass().getMethod("addNew"+se.getJavaPropertyName(),new
> >                 Class[0]);
> >                 for (int n = 0; n < se.getMinOccurs().intValue(); ++n )
> >                 {
> >                     Object val = m.invoke(o,new Object[0]);
> >                     if (val instanceof XmlObject) {
> >                         initializeBean((XmlObject)val,se.getType());
> >                         if (se.hasDefault() ==
> >                         SchemaProperty.CONSISTENTLY) {
> >                            
> > ((XmlAnySimpleType)val).setStringValue(se.getDefaultText());
> >                         }
> >                     }
> >                 }
> >             }
> >             catch (Exception e) {
> >                 log.warn(e);
> >             }
> >         }
> >     }
> > 
> > You could probably lose the schema type parameter( I needed it for my
> > app )and I havnt covered all permutations, . It works
> > by filling out any default attributes and also creates a number of child
> > elements equal to the minOccurs values. It uses the addNew
> > method defined in the bean to do this, as I was having trouble by purely
> > using a cursor.
> > 
> > I also wrapped up my beans with a java proxy so that everytime a new
> > bean is created using any of the interface methods it 
> > jumps in and initializes them ( I can send or post this code as well,
> > but there would probably be some specific stuff to my app in there) . It
> > all works pretty well, however it would be even better to have some way
> > of flagging the xmlBeans system to do this for you.
> > 
> > Cheers,
> > Marius Gleeson.
> > 
> > 
> > On Thu, 3 Feb 2005 10:13:12 -0800 (PST), "Martin Wegner"
> > <ma...@yahoo.com> said:
> > > 
> > > Is there a definitive advice as to the use of XML Schemea default
> > values
> > > and XMLBeans?  Is it true that the user of XMLBeans has to manually
> > add
> > > the default values after the parsing?  Is that the recommended
> > practive?
> > > 
> > > Thanks.
> > > 
> > > 
> > > --Marty
> > > 
> > > 
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > > For additional commands, e-mail: user-help@xmlbeans.apache.org
> > > 
> > -- 
> >   Marius Gleeson
> >   audumla@fastmail.fm
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: user-help@xmlbeans.apache.org
> > 
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
> 
-- 
  Marius Gleeson
  audumla@fastmail.fm


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


Re: Default Values and XMLBeans

Posted by Martin Wegner <ma...@yahoo.com>.
Marius,

That is very useful.  Do you not bother with travesing the entire DOM? 
Your solution does not walk all of the nodes in the XML tree.


--Marty

--- Marius Gleeson <xm...@subscribe.audumla.net> wrote:

> Unless something has changed recently that is right, you have to do it
> manually, however
> I have the same issue and so I wrote the following to help get
> initialized beans
> 
>     protected void initializeBean(XmlObject o, SchemaType st) {
>         SchemaProperty[] sp = st.getAttributeProperties();
>         XmlCursor c = o.newCursor();
>         for (int i = 0; i < sp.length; ++i ) {
>             if (sp[i].hasDefault() == SchemaProperty.CONSISTENTLY) {
>                
> c.setAttributeText(sp[i].getName(),sp[i].getDefaultText());
>             }
>         }
>         c.dispose();
>         sp = st.getElementProperties();
>         for (int i = 0; i < sp.length; ++i ) {
>             SchemaProperty se = sp[i];
>             try {
>                 Method m =
>                
> o.getClass().getMethod("addNew"+se.getJavaPropertyName(),new
>                 Class[0]);
>                 for (int n = 0; n < se.getMinOccurs().intValue(); ++n )
>                 {
>                     Object val = m.invoke(o,new Object[0]);
>                     if (val instanceof XmlObject) {
>                         initializeBean((XmlObject)val,se.getType());
>                         if (se.hasDefault() ==
>                         SchemaProperty.CONSISTENTLY) {
>                            
> ((XmlAnySimpleType)val).setStringValue(se.getDefaultText());
>                         }
>                     }
>                 }
>             }
>             catch (Exception e) {
>                 log.warn(e);
>             }
>         }
>     }
> 
> You could probably lose the schema type parameter( I needed it for my
> app )and I havnt covered all permutations, . It works
> by filling out any default attributes and also creates a number of child
> elements equal to the minOccurs values. It uses the addNew
> method defined in the bean to do this, as I was having trouble by purely
> using a cursor.
> 
> I also wrapped up my beans with a java proxy so that everytime a new
> bean is created using any of the interface methods it 
> jumps in and initializes them ( I can send or post this code as well,
> but there would probably be some specific stuff to my app in there) . It
> all works pretty well, however it would be even better to have some way
> of flagging the xmlBeans system to do this for you.
> 
> Cheers,
> Marius Gleeson.
> 
> 
> On Thu, 3 Feb 2005 10:13:12 -0800 (PST), "Martin Wegner"
> <ma...@yahoo.com> said:
> > 
> > Is there a definitive advice as to the use of XML Schemea default
> values
> > and XMLBeans?  Is it true that the user of XMLBeans has to manually
> add
> > the default values after the parsing?  Is that the recommended
> practive?
> > 
> > Thanks.
> > 
> > 
> > --Marty
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: user-help@xmlbeans.apache.org
> > 
> -- 
>   Marius Gleeson
>   audumla@fastmail.fm
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
> 
> 


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


Re: Default Values and XMLBeans

Posted by Marius Gleeson <xm...@subscribe.audumla.net>.
Unless something has changed recently that is right, you have to do it
manually, however
I have the same issue and so I wrote the following to help get
initialized beans

    protected void initializeBean(XmlObject o, SchemaType st) {
        SchemaProperty[] sp = st.getAttributeProperties();
        XmlCursor c = o.newCursor();
        for (int i = 0; i < sp.length; ++i ) {
            if (sp[i].hasDefault() == SchemaProperty.CONSISTENTLY) {
                c.setAttributeText(sp[i].getName(),sp[i].getDefaultText());
            }
        }
        c.dispose();
        sp = st.getElementProperties();
        for (int i = 0; i < sp.length; ++i ) {
            SchemaProperty se = sp[i];
            try {
                Method m =
                o.getClass().getMethod("addNew"+se.getJavaPropertyName(),new
                Class[0]);
                for (int n = 0; n < se.getMinOccurs().intValue(); ++n )
                {
                    Object val = m.invoke(o,new Object[0]);
                    if (val instanceof XmlObject) {
                        initializeBean((XmlObject)val,se.getType());
                        if (se.hasDefault() ==
                        SchemaProperty.CONSISTENTLY) {
                            ((XmlAnySimpleType)val).setStringValue(se.getDefaultText());
                        }
                    }
                }
            }
            catch (Exception e) {
                log.warn(e);
            }
        }
    }

You could probably lose the schema type parameter( I needed it for my
app )and I havnt covered all permutations, . It works
by filling out any default attributes and also creates a number of child
elements equal to the minOccurs values. It uses the addNew
method defined in the bean to do this, as I was having trouble by purely
using a cursor.

I also wrapped up my beans with a java proxy so that everytime a new
bean is created using any of the interface methods it 
jumps in and initializes them ( I can send or post this code as well,
but there would probably be some specific stuff to my app in there) . It
all works pretty well, however it would be even better to have some way
of flagging the xmlBeans system to do this for you.

Cheers,
Marius Gleeson.


On Thu, 3 Feb 2005 10:13:12 -0800 (PST), "Martin Wegner"
<ma...@yahoo.com> said:
> 
> Is there a definitive advice as to the use of XML Schemea default values
> and XMLBeans?  Is it true that the user of XMLBeans has to manually add
> the default values after the parsing?  Is that the recommended practive?
> 
> Thanks.
> 
> 
> --Marty
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
> 
-- 
  Marius Gleeson
  audumla@fastmail.fm


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