Hi,

This has already been proffered but I just checked the archives and they are
not available.

Does anyone know where I can get some code sample to "introspect" schemas? I
need to introspect a schema in order for me to  generate a correct XML
document from it.

Briefly, I'm trying to introspect a schema (which is really composed of a
number of files (i.e., via includes)) and I'm able to get the global
elements, attributes etc. but I'm unsure how go from there? That is how do I
build the contents of a complex type or element which itself might be
composed of child complex types etc? For example, I tried getting the
complex type content (i.e., method getEnclosingCTDefinition ()) for the
following element and it came back unexpectedly (for me) as null:

<element name = "Customer">
        <complexType>
                <sequence>
                        <element name = "FirstName" type = "string" />
                        <element name = "MiddleInitial" type = "string" />
                        <element name = "LastName" type = "string" />
                </sequence>
        </complexType>
</element>


Here is a code snippet that gathers the schema grammar for me:

        XMLSchemaLoader xmlSchemaLoader = new XMLSchemaLoader();
        xmlSchemaLoader.setErrorHandler (this);

        XMLInputSource xmlInputSource = new XMLInputSource (null, schemaFile.
          getName (), schemaFile.toURI ().getPath ());

        FileInputStream inputStream = null;
        try
        {
                inputStream = new FileInputStream (schemaFile);
        }
        catch (Exception pException) { pException.printStackTrace (); }

        xmlInputSource.setByteStream (inputStream);

        XSGrammar schemaGrammar = null;
        try
        {
                schemaGrammar = (XSGrammar) xmlSchemaLoader.loadGrammar (
                  xmlInputSource);
                XSModel schemaModel = schemaGrammar.toXSModel();

                XSNamedMap elementComponents = pSchemaModel.getComponents (
                  XSConstants.ELEMENT_DECLARATION);
                System.out.println ("Element components begin (" + 
elementComponents.
                  getLength () + ")");

                for (int i = 0; i < elementComponents.getLength (); i++)
                {
                        XSElementDeclaration elementDeclaration = 
(XSElementDeclaration)
                          elementComponents.item (i);
                        System.out.println ("\tNamespace:Name: " + 
elementDeclaration.
                          getNamespace () + ":" + elementDeclaration.getName 
());
                        System.out.println ("\tConstraint value: \"" + 
elementDeclaration.
                          getConstraintValue() + "\"");

                        System.out.println ("\tType def name: \"" + 
elementDeclaration.
                          getTypeDefinition().getName () + "\"");

                        if (elementDeclaration.getEnclosingCTDefinition() != 
null)
                        {
                                System.out.println ("\tEnclosing ct type def 
name: \"" +
                                  
elementDeclaration.getEnclosingCTDefinition().getParticle ().
                                  getName () + "\"");
                        }
                        else
                        {
                                System.out.println ("\tNo complex type 
definition...");
                        }

                        System.out.println ();
                }
                System.out.println ("Element components end...\n");
        }
        catch (Exception pException)
        {
                pException.printStackTrace ();
        }

One final note: as I mentioned above my schema is really composed of a
number of files which are brought together by "include" statements and I'm
hoping that all I need to do is point Xerces to my "lead" .xsd file and that
all the included files will automatically be brought in.

Thanks.

_____________________________________________
Karl R. Mueller
[EMAIL PROTECTED]
_____________________________________________



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to