title: How to parse XSD and read all the complex elements and its child elements using the Java and XmlSchema Core
body: <p>I am using the <code>Apache XmlSchema Core</code> library to parse the <code>XSD</code> file and get all the elements and its children type (datatype, maxOccurs, etc). I am following the documentation <a href="http://ws.apache.org/xmlschema/xmlschema-core/apidocs/index.html" rel="nofollow noreferrer">Apache XML SCHEMA CORE</a> and trying to do. But after navigating to a certain point I am getting a bit confused. Can someone please guide me on how can I traverse through my <code>XSD</code> file and get all the elements and its child elements along with the related information?</p>
  <p>I am able to get all the XSD information in my <code>schema</code> element I just want to know how can I access the child elements from my root <code>RootFood</code> and get its related information. Any help would be really appreciated.</p>
  <p>I tried to continue further and this is what I have so far:
  The element <code>RootFood</code> belongs to the instance of the class <code>XmlSchemaGroupParticle</code>. I tried to debug the code to find the elements that are associated with my <code>rootParticles</code>, it has the field called <code>items</code> within which I have my <code>food</code> element <code>items-&gt;[0]-&gt;namedDelegate-&gt;qName-&gt;localPart</code> but when I try to add <code>GET</code> method on <code>rootParticles</code> to obtain the <code>items</code> then there is no such method.</p>
  <p>The <code>XmlSchemaParticle</code> extends following classes: <code>XmlSchemaAnnotated</code>, <code>XmlSchemaObject</code>, and implements the interface <code>XmlSchemaObjectBase</code> but none of them have the field called <code>Items</code>.</p>
  <p>Following is the Java code I have so far, I tried several things:</p>
  <pre><code>public class XMLSchemaCore {
  
      public static void main(String[] args) throws URISyntaxException,
          FileNotFoundException,
          UnsupportedEncodingException {
              String xsdPath = Paths.get(XMLSchemaCore.class.getClassLoader().getResource(&quot;test.xsd&quot;).toURI()).toFile().getAbsolutePath();
              String filePath = Path.of(xsdPath).toString();
  
              InputStream is = new FileInputStream(filePath);
              XmlSchemaCollection schemaCol = new XmlSchemaCollection();
  
              // Schema contain the complete XSD content which needs to be parsed
              XmlSchema schema = schemaCol.read(new StreamSource(is));
              // schema.write(System.out);
              for (Map.Entry&lt;QName, XmlSchemaElement&gt; entry: schema.getElements().entrySet()) {
                  // Root element and its contents
                  QName parentElementName = entry.getKey();
                  XmlSchemaElement parentElementValues = entry.getValue();
  
                  // Get the elements based on the Root element
                  XmlSchemaElement root = schema.getElementByName(parentElementName);
  
                  // Get the type of the root element
                  XmlSchemaType type = root != null ? root.getSchemaType() : null;
  
                  // Check if the root element is of Complex type
                  if (type instanceof XmlSchemaComplexType) {
                      // Get the Particles associated with the element
                      XmlSchemaParticle rootParticles = ((XmlSchemaComplexType) type).getParticle();
  
                      // Check particle belongs to which type
                      if (rootParticles instanceof XmlSchemaAny) {
                          System.out.println(&quot;Any Schema Type&quot;);
  
                      } else if (rootParticles instanceof XmlSchemaElement) {
                          System.out.println(&quot;Element Schema Type&quot;);
  
                      } else if (rootParticles instanceof XmlSchemaGroupParticle) {
                          System.out.println(&quot;Group Schema Type&quot;);
                          System.out.println(rootParticles);
                          System.out.println(rootParticles);
  
                      } else if (rootParticles instanceof XmlSchemaGroupRef) {
                          System.out.println(&quot;Group Ref Schema Type&quot;);
  
                      }
                  }
              }
  
          }
  
  }
  </code></pre>
  <p>Following is the <code>XSD</code> file I have:</p>
  <pre><code>&lt;xs:schema attributeFormDefault=&quot;unqualified&quot;
      elementFormDefault=&quot;qualified&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
      &lt;xs:element name=&quot;RootFood&quot;&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name=&quot;food&quot;&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;name&quot; /&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;price&quot; /&gt;
                              &lt;xs:element type=&quot;xs:string&quot; name=&quot;description&quot; /&gt;
                              &lt;xs:element type=&quot;xs:short&quot; name=&quot;calories&quot; /&gt;
                              &lt;xs:element name=&quot;ingredients&quot;&gt;
                                  &lt;xs:complexType&gt;
                                      &lt;xs:sequence&gt;
                                          &lt;xs:element type=&quot;xs:string&quot; name=&quot;ingredient&quot;
                                              maxOccurs=&quot;unbounded&quot; minOccurs=&quot;0&quot; /&gt;
                                      &lt;/xs:sequence&gt;
                                  &lt;/xs:complexType&gt;
                              &lt;/xs:element&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/728ik.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/728ik.jpg" alt="enter image description here" /></a></p>