Boy, I wish I got paid for this crap.

 

Full answer here:
http://www.oracle.com/technetwork/articles/entarch/incremental-compilation-x
mlbeans-089127.html

 

In your first example you had compiled your schema and created an object to
describe parsed XML.

 

In you second example you parsed the XSD, which is after all XML, and,
surprise, it's described by W3C XSD.

 

What you want to do, and I would be interested in hearing why, is to compile
the XSD on the fly.

 

The output of the example below is:

D=TestSchema

D=schema@http://www.w3.org/2001/XMLSchema

D=TestSchema

where the first is your first example, the second is your second example,
and the third is from compiling the XSD on the fly.

 

Hope this helps.

 

Cheers,

Paul

 

package com.riveralph;

 

import java.io.File;

 

import noNamespace.TestSchemaDocument;

 

import org.apache.xmlbeans.SchemaTypeLoader;

import org.apache.xmlbeans.SchemaTypeSystem;

import org.apache.xmlbeans.XmlBeans;

import org.apache.xmlbeans.XmlObject;

import org.apache.xmlbeans.XmlOptions;

import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;

 

public class TestSchema {

      public static void main(String[] args) throws Exception {

            TestSchema ts = new TestSchema();

            ts.go(args);

      }

 

      private void go(String[] args) throws Exception {

            {

                  XmlObject instance =
TestSchemaDocument.Factory.newInstance(new XmlOptions());

                  System.out.println(instance.schemaType());

            }

 

            {

                  SchemaTypeLoader loader =
XmlBeans.typeLoaderForClassLoader(SchemaDocument.class.getClassLoader());

                  XmlObject instance = loader.parse(new
File("xsd/testschema.xsd"),null, new XmlOptions());

                  System.out.println(instance.schemaType());

            }

 

            {

                  XmlObject[] schemaObj = new XmlObject[]   {
XmlObject.Factory.parse(new File("xsd/testschema.xsd"))};

                  SchemaTypeSystem schemaTypeObj =
XmlBeans.compileXmlBeans(null, null, schemaObj, null, null, null, null);

                  XmlObject instance =
schemaTypeObj.newInstance(schemaTypeObj.documentTypes()[0], null);

                  System.out.println(instance.schemaType());

            }

 

      }

}

 

 

From: badger mailinglist [mailto:badger.mailing.l...@gmail.com] 
Sent: Monday, January 14, 2013 6:35 AM
To: user@xmlbeans.apache.org
Subject: Re: Converting from using scomp to SchemaTypeLoader.parse(...)

 

Ok, so I guess no one has ever tried this.

Maybe there's a simpler question:

If I use the SchemaTypeLoader.parse method, I struggle to get information
about the schema from that object. I want to iterate through the attributes
and elements declared in the XSD I'm parsing, but all I seem to get is w3
xml schema stuff. Am I trying to do something that isn't supported, or am I
just doing it wrong?

Any help much appreciated,

Cheers,

Badger.

On 8 January 2013 15:42, badger mailinglist <badger.mailing.l...@gmail.com>
wrote:

Hi,

Currently, I take some XSDs (see below for example), generate classes for
them (with scomp -out generatedClasses.jar testschema.xsd), then use those
classes in code with some like:
final XmlObject instance =
TestSchemaDocument.Factory.newInstance(xmlOptions);

This process now needs to be a bit more dynamic, so I'm trying to use
something more like the following:

final SchemaTypeLoader loader =
XmlBeans.typeLoaderForClassLoader(SchemaDocument.class.getClassLoader());
XmlOptions xmlOptions = new XmlOptions();
final XmlObject fileParsedInstance = loader.parse(new
File("C:\\testschema.xsd"), null, new XmlOptions());


However, I would expect the XmlObject returned in both cases to be the same.
However, if I recursively iterate through the properties of these two
objects, I get different results i.e. calling
instance.schemaType().getProperties() gets different results in each of the
cases.
For the instance loaded from the class the getProperties() method returns
one element with type 'TestSchema', which itself returns one element with
type 'TestType' and so on as you recur.
For the instance loaded directly from the xsd file the properties are all
things like E=restriction|D=restriction@http://www.w3.org/2001/XMLSchema,
which has a property T=localSimpleType@http://www.w3.org/2001/XMLSchema,
which has a property 'restriction' and so on infinitely.

I've assumed that these two methods of loading the schema would do the same
thing, but it looks like I'm wrong, can someone point me at the method call
I need to make to load from the file properly? I'm using XmlBeans 2.5.0.

Thanks!


Example xsd I'm testing this with:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema";>

  <xs:element name="TestSchema" type="TestType" />
  
  <xs:complexType name="TestComplexType">
    <xs:sequence>
      <xs:element type="xs:string" name="thing" />
    </xs:sequence>
  </xs:complexType>
  
  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element type="TestComplexType" name="TestComplex"
      maxOccurs="unbounded" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
  
</xs:schema>

 

Reply via email to