I think what you actually want to be doing is the
following:
Object result = null;
Class destClass = Class.forName(beanId);
Class[] innerClasses = destClass.getClasses();
Class factory = null;
for (int i = 0; i < innerClasses.length; i++) {
if (innerClasses[i].getName().endsWith("Factory")) {
factory = innerClasses[i];
}
}
if (factory == null) {
throw new Exception("Factory class of Bean of type " + beanId + " not found.");
}
Class[] emptyArglist = new Class[0];
Method newInstance = factory.getMethod("newInstance", emptyArglist);
result = newInstance.invoke(null, emptyArglist);
This
was taken from the Dozer XmlBeanFactory that dynamically creates XmlObject
instances based upon String values of the class
name.
From: "Fermin Da Costa Gomez" <[EMAIL PROTECTED]> [mailto:"Fermin Da Costa Gomez" <[EMAIL PROTECTED]>]
Sent: Monday, March 12, 2007 10:19 AM
To: [email protected]
Subject: Re: Dynamically creating the elements in a structure
I haven't tried, but do you have problems with
Class.forName(someRootAsString).newInstance() ?
Correct. Assuming i'm not making some very silly misstake
The only thing I can think of that might cause problems would be in
the lookup of the xsbs.
Can you update teh list with what issues you have?
Here it goes:
<snip>
...
// This bit works just fine so the class in itself is known
FundDocument fundDoc= FundDocument.Factory.newInstance();
try {
String clazz1 = "Fund"+ "DocumentImpl";
Object xmlObject = Class.forName(clazz2).newInstance(); // Returns a ClassNotFound Exception
String clazz2 = "noNamespace."+ "Fund"+ "Document";
Object xmlObject = Class.forName(clazz2).newInstance(); // Returns an Instantiation Exception
} catch (Exception e) {
e.printStackTrace();
}
...
</snip>
What i would like to so is use the xmlObject in a generic fashion so i don't have to hardcode.
<snip>
...
// This bit works just fine so the class in itself is known
FundDocument fundDoc= FundDocument.Factory.newInstance();
try {
String clazz1 = "Fund"+ "DocumentImpl";
Object xmlObject = Class.forName(clazz2).newInstance(); // Returns a ClassNotFound Exception
String clazz2 = "noNamespace."+ "Fund"+ "Document";
Object xmlObject = Class.forName(clazz2).newInstance(); // Returns an Instantiation Exception
} catch (Exception e) {
e.printStackTrace();
}
...
</snip>
What i would like to so is use the xmlObject in a generic fashion so i don't have to hardcode.
Is this enough info? If not please let me know.
Tia
Fermin
> > I haven't seen a previous post on the list about this topic. That may
> > be why you didn't get an answer :)
> Hmm, that would explain it, indeed.
>
> What problems are you seeing when you try the code you list below?
> >
> > If I understand your problem correctly, you have something like:
> > <el>1</el1> and you want to determine what type that is so you can use
> > somenamespace.ElDocument as your type. Is this correct?
>
> No exactly. I think i have to be more clear (always a challenge but here it
> goes .. )
>
> I'v got the big jar that is produced when you run scomp on an xsd.
> When using the code in a normal fashion one would just get an object, say
> SomeRoot, and use the SomeRootDocument.Factory.someMethod()
> to accomplish whatever.
>
> Now, what i want to do is slightly different.
> Rather than using SomeDocumentRootDocument explicitly i am looking for a way
> to use the Class.forName(someRootAsString).newInstance()
> method to get my hands on an xmlbeans document that i specify via a string.
> This way i don't have to hardcode the construction of the xml but i could
> supply the structure via simple strings, read via a small input file.
> If this is possible one can construct any xml (sub)doc (based on the overall
> xsd) without having to code for it.
>
> Does this make sense?
>
> > Have you tried something like:
> > XmlObject xo = XmlObject.Factory.parse("<el>1</el1>");
> > and
> > xo.type
> > xo.type.getName
> Since this suggests the string being an xml element i have not because i
> would be looking for a className (the string thereof) to be handed of and
> return with an instance of type XmlObject.
> Basically i don't know where the xml is going to start so i just want to
> supply a string (name of the element to denote as root of the document to be
> build), create the corresponding document and build the rest of the xml-doc
> in the same fashion.
>
> > the scan the classes you expect to find a matching type
> > if(ElDocument.type.getName == xo.type.getName){
> > // do something
> > }
> >
> >
> > -Jacobd
> >
> > On 3/12/07, Fermin Da Costa Gomez <[EMAIL PROTECTED] > wrote:
> > > Hi,
> > >
> > > Just started on Xmlbeans and going through the list has not yielden an
> > > answer to my question.
> > >
> > > Is there a way to get a hold of a SomeDocument object by using
> > > Class.forName("namespace.SomeDocument").newInstance() .. ?
> > > Or any other way by means of just using a string that refers/ points to
> the
> > > element one is looking for?
> > >
> > > So i have got the name of a document/ node/ tag in the form of a string
> and
> > > i'm looking for an object based on that string.
> > >
> > > Tia,
> > >
> > > Fermin DCG
> > >
> > >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
>
>
> --
> "The reasonable man adapts himself to the world; the unreasonable one
> persists in trying to adapt the world to himself. Therefore all progress
> depends on the unreasonable man."
> - George Bernard Shaw (1856 - 1950)
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man."
- George Bernard Shaw (1856 - 1950) --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

