You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by al...@gmx.net on 2008/10/08 11:23:06 UTC

How to get Java Class of .xsd at runtime dynamically on-the-fly?

Hello,

I need the (freshly compiled?) Java Class from a .xsd at runtime
on-the-fly. Without creating java source code files or .jar files.

I tried org.apache.xmlbeans.XmlBeans.compileXsd()
and
org.apache.xmlbeans.XmlBeans.loadXsd()

but I could not figure out how to spawn a Class from it. I tried to use
the SchemaTypeSystem.getClassLoader() but this returns null always. I
also tried SchemaType.getJavaClass() but somehow it does not work/I
don't understand it/I don't know how to find the "root item Class" with it.

Can you tell me: how do I get the Java Class of the "root item" within
the .xsd at runtime (on-the-fly, without generating code files or .jar) ?

regards!

ali
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

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


RE: How to get Java Class of .xsd at runtime dynamically on-the-fly?

Posted by Radu Preotiuc-Pietro <ra...@oracle.com>.
Ali,

What you want actually is XmlBeans.compileXmlBeans(), which is documented, and it allows you to set Java code generation options and manage the location of the source files. Then, you can have Eclipse compile the files. The part of having a classloader able to handle the compiled files is tricky, but this is just Java at this point. I should mention that, aside from the compiled .class files, you need to save the SchemaTypeSystem that the compileXmlBeans method returns alongside them (it will be saved as .xsb files), because the code that XmlBeans generates needs them. When you have your classloader ready, I suggest you try it first with artifacts generated by the "scomp" script to make sure it works.

Hope this helps,
Radu 

> -----Original Message-----
> From: aliban@gmx.net [mailto:aliban@gmx.net] 
> Sent: Wednesday, October 22, 2008 12:14 PM
> To: user@xmlbeans.apache.org
> Subject: Re: How to get Java Class of .xsd at runtime 
> dynamically on-the-fly?
> 
> Hello Radu,
> 
> Thank you very much for your reply. I already managed to 
> compile a .xsd to java classes - however it appeared that the 
> XmlBeans.compileXsd() did not create Java source files. Thus 
> I tried hard to read and understand the code of scomp and 
> after some "reveres engineering" various undocumented classes 
> i found out that I get the Java source created when using 
> XmlBeans.compileXsd() with all the variables properly set but 
> not with compileXsd(). [Therefore I suggest to update the 
> docu about scomp where they point to the compileXsd() 
> function.] After the source was created I compile it with the 
> eclipse compiler: next I will write the class loader.
> 
> regards
> 
> Radu Preotiuc-Pietro schrieb:
> > Well, this is somewhat a Java question also. Each Java 
> class has to have a classloader associated to it. The only 
> way to get a Java class is to have a classloader that is able 
> to find the .class representation of a class and load it.
> >
> > What this means for XMLBeans is that you need to compile 
> the Schemas to Java source (XmlBeans.compileXsd() or call 
> "scomp" in a different process), then compile and jar that 
> source and then create a classloader that has access to the 
> freshly created .jar file. XmlBeans can't create classes 
> without source .java files and .jar files.
> >
> > What I should also point out is that you will only be able 
> to use such dynamically generated classes via reflection 
> (since they don't exist when your code is compiled), so this 
> is why most people prefer to use DOM or XmlCursor when 
> dealing with Schemas that they only know about at runtime. 
> The XML documents themselves will still be bound to a Schema, 
> so you'll be able to do Schema validation on them and get the 
> actual Schema types.
> >
> > Radu
> >
> >
> >> -----Original Message-----
> >> From: aliban@gmx.net [mailto:aliban@gmx.net]
> >> Sent: Wednesday, October 08, 2008 2:23 AM
> >> To: user@xmlbeans.apache.org
> >> Subject: How to get Java Class of .xsd at runtime dynamically 
> >> on-the-fly?
> >>
> >> Hello,
> >>
> >> I need the (freshly compiled?) Java Class from a .xsd at runtime 
> >> on-the-fly. Without creating java source code files or .jar files.
> >>
> >> I tried org.apache.xmlbeans.XmlBeans.compileXsd()
> >> and
> >> org.apache.xmlbeans.XmlBeans.loadXsd()
> >>
> >> but I could not figure out how to spawn a Class from it. I 
> tried to 
> >> use the SchemaTypeSystem.getClassLoader() but this returns null 
> >> always. I also tried SchemaType.getJavaClass() but somehow it does 
> >> not work/I don't understand it/I don't know how to find the "root 
> >> item Class" with it.
> >>
> >> Can you tell me: how do I get the Java Class of the "root item" 
> >> within the .xsd at runtime (on-the-fly, without generating 
> code files 
> >> or .jar) ?
> >>
> >> regards!
> >>
> >> ali
> >> --
> >> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
> >> Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
> >>
> >> 
> ---------------------------------------------------------------------
> >> 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
> >
> >
> 
> 
> ---------------------------------------------------------------------
> 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: How to get Java Class of .xsd at runtime dynamically on-the-fly?

Posted by "aliban@gmx.net" <al...@gmx.net>.
Hello Radu,

Thank you very much for your reply. I already managed to compile a .xsd 
to java classes - however it appeared that the XmlBeans.compileXsd() did 
not create Java source files. Thus I tried hard to read and understand 
the code of scomp and after some "reveres engineering" various 
undocumented classes i found out that I get the Java source created when 
using XmlBeans.compileXsd() with all the variables properly set but not 
with compileXsd(). [Therefore I suggest to update the docu about scomp 
where they point to the compileXsd() function.] After the source was 
created I compile it with the eclipse compiler: next I will write the 
class loader.

regards

Radu Preotiuc-Pietro schrieb:
> Well, this is somewhat a Java question also. Each Java class has to have a classloader associated to it. The only way to get a Java class is to have a classloader that is able to find the .class representation of a class and load it.
>
> What this means for XMLBeans is that you need to compile the Schemas to Java source (XmlBeans.compileXsd() or call "scomp" in a different process), then compile and jar that source and then create a classloader that has access to the freshly created .jar file. XmlBeans can't create classes without source .java files and .jar files.
>
> What I should also point out is that you will only be able to use such dynamically generated classes via reflection (since they don't exist when your code is compiled), so this is why most people prefer to use DOM or XmlCursor when dealing with Schemas that they only know about at runtime. The XML documents themselves will still be bound to a Schema, so you'll be able to do Schema validation on them and get the actual Schema types.
>
> Radu
>
>   
>> -----Original Message-----
>> From: aliban@gmx.net [mailto:aliban@gmx.net] 
>> Sent: Wednesday, October 08, 2008 2:23 AM
>> To: user@xmlbeans.apache.org
>> Subject: How to get Java Class of .xsd at runtime dynamically 
>> on-the-fly?
>>
>> Hello,
>>
>> I need the (freshly compiled?) Java Class from a .xsd at 
>> runtime on-the-fly. Without creating java source code files 
>> or .jar files.
>>
>> I tried org.apache.xmlbeans.XmlBeans.compileXsd()
>> and
>> org.apache.xmlbeans.XmlBeans.loadXsd()
>>
>> but I could not figure out how to spawn a Class from it. I 
>> tried to use the SchemaTypeSystem.getClassLoader() but this 
>> returns null always. I also tried SchemaType.getJavaClass() 
>> but somehow it does not work/I don't understand it/I don't 
>> know how to find the "root item Class" with it.
>>
>> Can you tell me: how do I get the Java Class of the "root 
>> item" within the .xsd at runtime (on-the-fly, without 
>> generating code files or .jar) ?
>>
>> regards!
>>
>> ali
>> --
>> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
>> Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
>>
>> ---------------------------------------------------------------------
>> 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
>
>   


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


RE: RE: How to get Java Class of .xsd at runtime dynamically on-the-fly?

Posted by Radu Preotiuc-Pietro <ra...@oracle.com>.
Yeah, keep in mind that there can be multiple root elements in a Schema, so use SchemaTypeSystem.documentTypes() to enumerate all of them on the SchemaTypeSystem object returned from compileXmlBeans.

Radu

> -----Original Message-----
> From: Julian Kölle [mailto:aliban@gmx.net] 
> Sent: Thursday, October 23, 2008 2:22 AM
> To: user@xmlbeans.apache.org
> Subject: Re: RE: How to get Java Class of .xsd at runtime 
> dynamically on-the-fly?
> 
> Hi again,
> 
> I have another question: when XML Beans creates the java 
> source files with a .xsd, is it possible to identify/know the 
> java class name of the root element defined int the XML 
> Schema? Because I must load this class at runtime i must know 
> its name. Can you point me to a solution for this?
> 
> regards
> 
> 
> -------- Original-Nachricht --------
> > Datum: Tue, 21 Oct 2008 17:52:00 -0700
> > Von: "Radu Preotiuc-Pietro" <ra...@oracle.com>
> > An: "user@xmlbeans.apache.org" <us...@xmlbeans.apache.org>
> > Betreff: RE: How to get Java Class of .xsd at runtime 
> dynamically on-the-fly?
> 
> > Well, this is somewhat a Java question also. Each Java class has to 
> > have a classloader associated to it. The only way to get a 
> Java class 
> > is to have a classloader that is able to find the .class 
> > representation of a class and load it.
> >
> > What this means for XMLBeans is that you need to compile 
> the Schemas 
> > to Java source (XmlBeans.compileXsd() or call "scomp" in a 
> different 
> > process), then compile and jar that source and then create a 
> > classloader that has access to the freshly created .jar 
> file. XmlBeans 
> > can't create classes without source .java files and .jar files.
> >
> > What I should also point out is that you will only be able 
> to use such 
> > dynamically generated classes via reflection (since they 
> don't exist 
> > when your code is compiled), so this is why most people 
> prefer to use 
> > DOM or XmlCursor when dealing with Schemas that they only 
> know about 
> > at runtime. The XML documents themselves will still be bound to a 
> > Schema, so you'll be able to do Schema validation on them 
> and get the actual Schema types.
> >
> > Radu
> >
> > > -----Original Message-----
> > > From: aliban@gmx.net [mailto:aliban@gmx.net]
> > > Sent: Wednesday, October 08, 2008 2:23 AM
> > > To: user@xmlbeans.apache.org
> > > Subject: How to get Java Class of .xsd at runtime dynamically 
> > > on-the-fly?
> > >
> > > Hello,
> > >
> > > I need the (freshly compiled?) Java Class from a .xsd at runtime 
> > > on-the-fly. Without creating java source code files or .jar files.
> > >
> > > I tried org.apache.xmlbeans.XmlBeans.compileXsd()
> > > and
> > > org.apache.xmlbeans.XmlBeans.loadXsd()
> > >
> > > but I could not figure out how to spawn a Class from it. 
> I tried to 
> > > use the SchemaTypeSystem.getClassLoader() but this returns null 
> > > always. I also tried SchemaType.getJavaClass() but 
> somehow it does 
> > > not work/I don't understand it/I don't know how to find the "root 
> > > item Class" with it.
> > >
> > > Can you tell me: how do I get the Java Class of the "root item" 
> > > within the .xsd at runtime (on-the-fly, without generating code 
> > > files or .jar) ?
> > >
> > > regards!
> > >
> > > ali
> > > --
> > > Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
> > > Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
> > >
> > > 
> --------------------------------------------------------------------
> > > - 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
> 
> --
> GMX Kostenlose Spiele: Einfach online spielen und Spaß haben 
> mit Pastry Passion!
> http://games.entertainment.gmx.net/de/entertainment/games/free
> /puzzle/6169196
> 
> ---------------------------------------------------------------------
> 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: RE: How to get Java Class of .xsd at runtime dynamically on-the-fly?

Posted by Julian Kölle <al...@gmx.net>.
Hi again,

I have another question: when XML Beans creates the java source files with a .xsd, is it possible to identify/know the java class name of the root element defined int the XML Schema? Because I must load this class at runtime i must know its name. Can you point me to a solution for this?

regards


-------- Original-Nachricht --------
> Datum: Tue, 21 Oct 2008 17:52:00 -0700
> Von: "Radu Preotiuc-Pietro" <ra...@oracle.com>
> An: "user@xmlbeans.apache.org" <us...@xmlbeans.apache.org>
> Betreff: RE: How to get Java Class of .xsd at runtime dynamically on-the-fly?

> Well, this is somewhat a Java question also. Each Java class has to have a
> classloader associated to it. The only way to get a Java class is to have
> a classloader that is able to find the .class representation of a class and
> load it.
> 
> What this means for XMLBeans is that you need to compile the Schemas to
> Java source (XmlBeans.compileXsd() or call "scomp" in a different process),
> then compile and jar that source and then create a classloader that has
> access to the freshly created .jar file. XmlBeans can't create classes without
> source .java files and .jar files.
> 
> What I should also point out is that you will only be able to use such
> dynamically generated classes via reflection (since they don't exist when your
> code is compiled), so this is why most people prefer to use DOM or
> XmlCursor when dealing with Schemas that they only know about at runtime. The XML
> documents themselves will still be bound to a Schema, so you'll be able to
> do Schema validation on them and get the actual Schema types.
> 
> Radu
> 
> > -----Original Message-----
> > From: aliban@gmx.net [mailto:aliban@gmx.net] 
> > Sent: Wednesday, October 08, 2008 2:23 AM
> > To: user@xmlbeans.apache.org
> > Subject: How to get Java Class of .xsd at runtime dynamically 
> > on-the-fly?
> > 
> > Hello,
> > 
> > I need the (freshly compiled?) Java Class from a .xsd at 
> > runtime on-the-fly. Without creating java source code files 
> > or .jar files.
> > 
> > I tried org.apache.xmlbeans.XmlBeans.compileXsd()
> > and
> > org.apache.xmlbeans.XmlBeans.loadXsd()
> > 
> > but I could not figure out how to spawn a Class from it. I 
> > tried to use the SchemaTypeSystem.getClassLoader() but this 
> > returns null always. I also tried SchemaType.getJavaClass() 
> > but somehow it does not work/I don't understand it/I don't 
> > know how to find the "root item Class" with it.
> > 
> > Can you tell me: how do I get the Java Class of the "root 
> > item" within the .xsd at runtime (on-the-fly, without 
> > generating code files or .jar) ?
> > 
> > regards!
> > 
> > ali
> > --
> > Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
> > Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
> > 
> > ---------------------------------------------------------------------
> > 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

-- 
GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion!
http://games.entertainment.gmx.net/de/entertainment/games/free/puzzle/6169196

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


RE: How to get Java Class of .xsd at runtime dynamically on-the-fly?

Posted by Radu Preotiuc-Pietro <ra...@oracle.com>.
Well, this is somewhat a Java question also. Each Java class has to have a classloader associated to it. The only way to get a Java class is to have a classloader that is able to find the .class representation of a class and load it.

What this means for XMLBeans is that you need to compile the Schemas to Java source (XmlBeans.compileXsd() or call "scomp" in a different process), then compile and jar that source and then create a classloader that has access to the freshly created .jar file. XmlBeans can't create classes without source .java files and .jar files.

What I should also point out is that you will only be able to use such dynamically generated classes via reflection (since they don't exist when your code is compiled), so this is why most people prefer to use DOM or XmlCursor when dealing with Schemas that they only know about at runtime. The XML documents themselves will still be bound to a Schema, so you'll be able to do Schema validation on them and get the actual Schema types.

Radu

> -----Original Message-----
> From: aliban@gmx.net [mailto:aliban@gmx.net] 
> Sent: Wednesday, October 08, 2008 2:23 AM
> To: user@xmlbeans.apache.org
> Subject: How to get Java Class of .xsd at runtime dynamically 
> on-the-fly?
> 
> Hello,
> 
> I need the (freshly compiled?) Java Class from a .xsd at 
> runtime on-the-fly. Without creating java source code files 
> or .jar files.
> 
> I tried org.apache.xmlbeans.XmlBeans.compileXsd()
> and
> org.apache.xmlbeans.XmlBeans.loadXsd()
> 
> but I could not figure out how to spawn a Class from it. I 
> tried to use the SchemaTypeSystem.getClassLoader() but this 
> returns null always. I also tried SchemaType.getJavaClass() 
> but somehow it does not work/I don't understand it/I don't 
> know how to find the "root item Class" with it.
> 
> Can you tell me: how do I get the Java Class of the "root 
> item" within the .xsd at runtime (on-the-fly, without 
> generating code files or .jar) ?
> 
> regards!
> 
> ali
> --
> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
> Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
> 
> ---------------------------------------------------------------------
> 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