users@jaxb.java.net

Re: JaxB and DOM dictionary

From: Ganga Sah <ganga_sah_at_yahoo.com>
Date: Thu, 05 Jun 2003 10:05:41 -0700

I also realize that examples would explain this
better.
In my application I had a need to get access to all
JAXB Java content tree mapped from XML element or
attribute. Here is an example:
If I have an instance of any base class say,
baseContentObject(which is a JAXB Java content object
already unmarshalled from XML instance doc) and I want
to iterate thru all content object inside it, I can do
this as below:
....
....
PropertyDescriptor[] propertyDescriptors =
PropertyUtil.getPropertyDescriptors(baseContentObject);
for (int i = 0; i < propertyDescriptors.length; i++) {
  if (propertyName.startsWith("primaryInterface"))
continue;//ignore unwanted system property
  String propertyName =
propertyDescriptors[i].getName();
  Method readMethod =
propertyDescriptors[i].getReadMethod();
  try {
    Object propertyValue =
readMethod.invoke(baseContentObject, null);
    System.out.println("baseContentObject has
property[" + i + "]: name=" + propertyName + "value="
+ propertyValue);
    //do application specific processing on this
property(which is another java content tree)
  } catch (IllegalAccessException illAccEx) {
    illAccEx.printStackTrace();
  } catch (InvocationTargetException invTarEx) {
    invTarEx.printStackTrace();
  }
....
....
where PropertyUtil.getPropertyDescriptors() was as
attached in my last email, which is as below:
public static PropertyDescriptor[]
getPropertyDescriptors(Object obj) {
    if (obj == null) return null;
    Class bean = obj.getClass();
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(bean,
Object.class);
    } catch(IntrospectionException e) {
        System.err.println("Couldn't introspect " +
bean.getName());
        return null;
    }
    return beanInfo.getPropertyDescriptors();
}

Hope this helps.
--ganga

--- Jerome Candat <jerome.candat_at_C-S.FR> wrote:
> On Wed, 4 Jun 2003 12:10:42 -0700, Ganga Sah
> <ganga_sah_at_yahoo.com> wrote:
>
> >Here is the futher deatils on my approach to this
> >problem.
> >I am attaching PropertyUtil class which has methods
> to
> >find property value(JAXB Java content object)
> inside
> >any base object(also JAXB Java content object,
> which
> >could be the root of Java content tree) with a
> given
> >one propertyName or an array of propertyName(s).
> >The methods returns the first object matching the
> >propertyName, which can be enhanced to return a
> list
> >of all matching objects.
> >Hope this helps.
> >
> >(I have extraced these methods with some twicking
> out
> >of my application, and have not tested it
> seperately).
> >
> >--ganga
>
> Sorry but I do not understand how you "attach" your
> PropertyUtil class inside your base object. Could
> you give an example please?
>
> Thanks in advance.
> J?r?me.


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com