title: Parsing XSD Schema with XSOM in Java. How to access element and complex types
body: <p>I’m having a lot of difficuly parsing an .XSD file with a XSOM in Java.  I have two .XSD files one defines a calendar and the second the global types. I'd like to be able to read the calendar file and determine that:</p>
  
  <p>calendar has 3 properties</p>
  
  <ul>
  <li>Valid is an ENUM called eYN</li>
  <li>Cal  is a String</li>
  <li>Status is a ENUM called eSTATUS</li>
  </ul>
  
  <p><strong>Calendar.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:gtypes="http://www.btec.com/gtypes"
   elementFormDefault="qualified"&gt;
  &lt;xs:import namespace="http://www.btec.com/gtypes"
   schemaLocation="gtypes.xsd"/&gt;
  &lt;xs:element name="CALENDAR"&gt;
    &lt;xs:complexType&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="Valid" type="eYN" minOccurs="0"/&gt;
        &lt;xs:element name="Cal" minOccurs="0"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:simpleContent&gt;
              &lt;xs:extension base="gtypes:STRING"&gt;
                &lt;xs:attribute name="IsKey" type="xs:string" fixed="Y"/&gt;
              &lt;/xs:extension&gt;
            &lt;/xs:simpleContent&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
        &lt;xs:element name="Status" type="eSTATUS" minOccurs="0"/&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:complexType name="eSTATUS"&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="gtypes:ENUM"/&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="eYN"&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="gtypes:ENUM"/&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p><strong>gtypes.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://www.btec.com/gtypes"
   elementFormDefault="qualified"&gt;
  &lt;xs:complexType name="ENUM"&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="xs:string"&gt;
        &lt;xs:attribute name="TYPE" fixed="ENUM"/&gt;
        &lt;xs:attribute name="derived" use="optional"/&gt;
        &lt;xs:attribute name="readonly" use="optional"/&gt;
        &lt;xs:attribute name="required" use="optional"/&gt;
      &lt;/xs:extension&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="STRING"&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="xs:string"&gt;
        &lt;xs:attribute name="TYPE" use="optional"/&gt;
        &lt;xs:attribute name="derived" use="optional"/&gt;
        &lt;xs:attribute name="readonly" use="optional"/&gt;
        &lt;xs:attribute name="required" use="optional"/&gt;
      &lt;/xs:extension&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>The code from my attempt to access this information is below. I'm pretty new to Java so 
  any style criticism welcome. </p>
  
  <p>I really need to know</p>
  
  <ol>
  <li>How to I access the complex type cal and see that it's a string?</li>
  <li>How do I access the definition of Status to see it's a enumeration of type eSTATUS 
  <em>emphasized text</em></li>
  </ol>
  
  <p>I've has several attempts to access the right information via ComplexType and Elements and Content. However I'm just don't get it and I cannot find any examples that help. I expect (hope) the best method is (relatively) simple when you know how. So, once again, if anyone could point me in the right direction that would be a great help.</p>
  
  <pre><code>xmlfile = "Calendar.xsd"
  XSOMParser parser = new XSOMParser();
  
  parser.parse(new File(xmlfile));
  XSSchemaSet sset = parser.getResult();
  XSSchema s = sset.getSchema(1);
  if (s.getTargetNamespace().equals("")) // this is the ns with all the stuff
         // in
  {
    // try ElementDecls
    Iterator jtr = s.iterateElementDecls();
    while (jtr.hasNext())
    {
      XSElementDecl e = (XSElementDecl) jtr.next();
      System.out.print("got ElementDecls " + e.getName());
      // ok we've got a CALENDAR.. what next?
      // not this anyway
      /*  
       *
       * XSParticle[] particles = e.asElementDecl() for (final XSParticle p :
       * particles) { final XSTerm pterm = p.getTerm(); if
       * (pterm.isElementDecl()) { final XSElementDecl ed =
       * pterm.asElementDecl(); System.out.println(ed.getName()); }
       */
    }
  
    // try all Complex Types in schema
    Iterator&lt;XSComplexType&gt; ctiter = s.iterateComplexTypes();
    while (ctiter.hasNext())
    {
      // this will be a eSTATUS. Lets type and get the extension to 
      // see its a ENUM
      XSComplexType ct = (XSComplexType) ctiter.next();
      String typeName = ct.getName();
      System.out.println(typeName + newline);
  
      // as Content
      XSContentType content = ct.getContentType();
      // now what?
      // as Partacle?
      XSParticle p2 = content.asParticle();
      if (null != p2)
      {
        System.out.print("We got partical thing !" + newline);
        // might would be good if we got here but we never do :-(
      }
  
      // try complex type Element Decs
      List&lt;XSElementDecl&gt; el = ct.getElementDecls();
      for (XSElementDecl ed : el)
      {
        System.out.print("We got ElementDecl !" + ed.getName() + newline);
        // would be good if we got here but we never do :-(
      }
  
      Collection&lt;? extends XSAttributeUse&gt; c = ct.getAttributeUses();
      Iterator&lt;? extends XSAttributeUse&gt; i = c.iterator();
      while (i.hasNext())
      {
        XSAttributeDecl attributeDecl = i.next().getDecl();
        System.out.println("type: " + attributeDecl.getType());
        System.out.println("name:" + attributeDecl.getName());
      }
    }
  }
  </code></pre>