Re: Question about generating Java Source Code from XML Schemas
Michael Bishop <[email protected]> Wed, 21 Aug 2013 11:49:14 -0400
| Newsgroups | gmane.text.xml.xmlbeans.user |
|---|---|
| Message-ID | <CAAenQJc6fetZbUowLvaDimasYWy2aXeLRh+oVW5fYdWSpKhRSg@mail.gmail.com> |
--20cf307f3bfa82c98004e4771d8c
Content-Type: text/plain; charset=ISO-8859-1
I think I can answer your first question. From what I recall in
parsing/reading documents, InputStream is a poor choice for XML documents
that include other documents. An InputStream object can't tell you where
its source is. Therefore, there is no way to find relative paths. I would
try the following:
final URL schema1Url = BeanGenerator.class.getResource("xsd/schema1.xsd");
final XmlObject schema1 = XmlObject.Factory.parse(schema1Url);
See if that helps.
On Wed, Aug 21, 2013 at 9:39 AM, Michael Szalay <[email protected]>wrote:
> Hi all
>
> I have some xsd schemas in my source tree and I want to generate java
> sources with XML Beans.
> I cannot use ant or maven task, I have to do it in plain java.
>
> I use the following java code:
>
> final InputStream schema1Stream =
> BeanGenerator.class.getResourceAsStream("xsd/schema1.xsd");
> final XmlObject schema1 = XmlObject.Factory.parse(schema1Stream);
> final InputStream schema2Stream =
> BeanGenerator.class.getResourceAsStream("xsd/schema2.xsd");
> final XmlObject schema2 = XmlObject.Factory.parse(schema2Stream);
> final XmlObject[] schemas = { schema1, schema2 };
> final BindingConfig config = new BindingConfig() {
> @Override
> public String lookupPackageForNamespace(String uri) {
> return "ch.mypackage";
> }
> };
>
> final Filer filer = new FilerImpl(null, new File(".java/src/"), null,
> true, true);
> final XmlOptions options = new XmlOptions();
> options.setCompileDownloadUrls();
> XmlBeans.compileXmlBeans(null, null, schemas, config, null, filer,
> options);
>
>
> I have questions:
>
> 1) The schemas have include statements like this:
>
> <xs:include schemaLocation="./th000008_extern.xsd"/>
>
> How can I make XmlBeans to include this schemas? Its located in the same
> folder as the original schema, but XMLBeans does not
> find it:
>
> URL "./th000008_extern.xsd" is not well-formed
>
> how to solve that?
>
> 2) I want everything XmlBeans generates in the package "ch.mypackage".
> How can I to this? XmlBeans generates a lot of other stuff like xsb
> files which are not in that package.
>
> How can I say to XmlBeans to not generate this files?
>
> 3) When I use one of the generated classes, there is the following
> runtime error:
>
> java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load
> class with name
>
> schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSystemHolder.
> Make sure the generated binary files are on the classpath.
> at
> org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:783)
> ... 29 more
> Caused by: java.lang.ClassNotFoundException:
>
> schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSystemHolder
> at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
> at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
> at
> org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:769)
> ... 30 more
>
> What is the problem here? Why is this class not generated?
>
> Thanks for a response
> Michael
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
--20cf307f3bfa82c98004e4771d8c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I think I can answer your first question. From what I reca=
ll in parsing/reading documents, InputStream is a poor choice for XML docum=
ents that include other documents. An InputStream object can't tell you=
where its source is. Therefore, there is no way to find relative paths. I =
would try the following:<br>
<br>final URL schema1Url =3D BeanGenerator.class.getResource("xsd/sche=
ma1.xsd");<br><div id=3D":m5">
final XmlObject schema1 =3D XmlObject.Factory.parse(schema1Url);<br><br></d=
iv><div id=3D":m5">See if that helps.<br></div><br></div><div class=3D"gmai=
l_extra"><br><br><div class=3D"gmail_quote">On Wed, Aug 21, 2013 at 9:39 AM=
, Michael Szalay <span dir=3D"ltr"><<a href=3D"mailto:Michael.Szalay@aba=
cus.ch" target=3D"_blank">[email protected]</a>></span> wrote:<br=
>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi all<br>
<br>
I have some xsd schemas in my source tree and I want to generate java<br>
sources with XML Beans.<br>
I cannot use ant or maven task, I have to do it in plain java.<br>
<br>
I use the following java code:<br>
<br>
final InputStream schema1Stream =3D<br>
BeanGenerator.class.getResourceAsStream("xsd/schema1.xsd");<br>
final XmlObject schema1 =3D XmlObject.Factory.parse(schema1Stream);<br>
final InputStream schema2Stream =3D<br>
BeanGenerator.class.getResourceAsStream("xsd/schema2.xsd");<br>
final XmlObject schema2 =3D XmlObject.Factory.parse(schema2Stream);<br>
final XmlObject[] schemas =3D { schema1, schema2 };<br>
final BindingConfig config =3D new BindingConfig() {<br>
=A0 =A0@Override<br>
=A0 =A0public String lookupPackageForNamespace(String uri) {<br>
=A0 =A0 =A0return "ch.mypackage";<br>
=A0 =A0}<br>
};<br>
<br>
final Filer filer =3D new FilerImpl(null, new File(".java/src/"),=
null,<br>
true, true);<br>
final XmlOptions options =3D new XmlOptions();<br>
options.setCompileDownloadUrls();<br>
XmlBeans.compileXmlBeans(null, null, schemas, config, null, filer,<br>
options);<br>
<br>
<br>
I have questions:<br>
<br>
1) The schemas have include statements like this:<br>
<br>
=A0 =A0<xs:include schemaLocation=3D"./th000008_extern.xsd"/&g=
t;<br>
<br>
How can I make XmlBeans to include this schemas? Its located in the same<br=
>
folder as the original schema, but XMLBeans does not<br>
find it:<br>
<br>
URL "./th000008_extern.xsd" is not well-formed<br>
<br>
how to solve that?<br>
<br>
2) I want everything XmlBeans generates in the package "ch.mypackage&q=
uot;.<br>
How can I to this? XmlBeans generates a lot of other stuff like xsb<br>
files which are not in that package.<br>
<br>
How can I say to XmlBeans to not generate this files?<br>
<br>
3) When I use one of the generated classes, there is the following<br>
runtime error:<br>
<br>
java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load<br=
>
class with name<br>
schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSyst=
emHolder.<br>
Make sure the generated binary files are on the classpath.<br>
=A0 =A0 at<br>
org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:783)<br=
>
=A0 =A0 ... 29 more<br>
Caused by: java.lang.ClassNotFoundException:<br>
schemaorg_apache_xmlbeans.system.s2EF0617F3F8756BE32108E3CF22693B4.TypeSyst=
emHolder<br>
=A0 =A0 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)<br>
=A0 =A0 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)<br>
=A0 =A0 at java.security.AccessController.doPrivileged(Native Method)<br>
=A0 =A0 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)<br>
=A0 =A0 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)<br>
=A0 =A0 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)<br=
>
=A0 =A0 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)<br>
=A0 =A0 at<br>
org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:769)<br=
>
=A0 =A0 ... 30 more<br>
<br>
What is the problem here? Why is this class not generated?<br>
<br>
Thanks for a response<br>
Michael<br>
<br>
<br>
<br>
---------------------------------------------------------------------<br>
To unsubscribe, e-mail: <a href=3D"mailto:[email protected].=
org">[email protected]</a><br>
For additional commands, e-mail: <a href=3D"mailto:[email protected]=
e.org">[email protected]</a><br>
<br>
</blockquote></div><br></div>
--20cf307f3bfa82c98004e4771d8c--