How to parse xsd String to fetch its elements using java?
<p>I need a solution on "How to parse a xml file and its xsd element's values"? </p>

<p>As a result of one of my web service method call, I will get an Java object which contains an xsd as Java String. This xsd contains many xsd:elements. I need to fetch one of the element called "AccountName" which contains a list of AccountNames in it. I need to use Java code here to fetch all this and store into one List. Here it is:</p>

<pre><code>metadataXsd -----------&gt;
&lt;xsd:schema xmlns:xsd="http://www.w3.org    /2001/XMLSchema"&gt;
   &lt;xsd:element name="metadata"&gt;
    &lt;xsd:complexType&gt;
  &lt;xsd:sequence&gt;
    &lt;xsd:element id="md_37EE5DE8-996B-0371-A119-61B77FE6ABCF" name="Category" minOccurs="0" maxOccurs="1"&gt;
      &lt;xsd:annotation&gt;
        &lt;xsd:documentation/&gt;
        &lt;xsd:appinfo&gt;
          &lt;label&gt;Category&lt;/label&gt;
          &lt;key&gt;Category&lt;/key&gt;
          &lt;searchable&gt;true&lt;/searchable&gt;
          &lt;timeControl&gt;false&lt;/timeControl&gt;
          &lt;description&gt;Specify categories where the media content should be published&lt;/description&gt;
        &lt;/xsd:appinfo&gt;
      &lt;/xsd:annotation&gt;
      &lt;xsd:simpleType&gt;
        &lt;xsd:restriction base="listType"&gt;
          &lt;xsd:enumeration value="Industries &amp;amp; Solutions"/&gt;
          &lt;xsd:enumeration value="Industries &amp;amp; Solutions - Solutions Topics"/&gt;
          &lt;xsd:enumeration value="Industries &amp;amp; Solutions - Industry Solutions"/&gt;
          &lt;xsd:enumeration value="Industries &amp;amp; Solutions - Business Partners and Alliances"/&gt;
          &lt;xsd:enumeration value="Services"/&gt;
          &lt;xsd:enumeration value="Services - Business Services"/&gt;
          &lt;xsd:enumeration value="Services - IT Services"/&gt;
          &lt;xsd:enumeration value="Services - Outsourcing Services"/&gt;
          &lt;xsd:enumeration value="Services - Training"/&gt;
          &lt;xsd:enumeration value="Services - Additional Services"/&gt;
          &lt;xsd:enumeration value="Products"/&gt;
          &lt;xsd:enumeration value="Products - Software"/&gt;
          &lt;xsd:enumeration value="Products - Systems"/&gt;
          &lt;xsd:enumeration value="Products - Storage"/&gt;
          &lt;xsd:enumeration value="Products - Additional Products"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Knowledge Center"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Knowledge Center - Education Assistant"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Technical Support"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Developer Support"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Supplier Support"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Former Products"/&gt;
          &lt;xsd:enumeration value="Support &amp;amp; downloads - Downloads"/&gt;
          &lt;xsd:enumeration value="My"/&gt;
          &lt;xsd:enumeration value="Interests"/&gt;
        &lt;/xsd:restriction&gt;
      &lt;/xsd:simpleType&gt;
    &lt;/xsd:element&gt;
    &lt;xsd:element id="md_ACECFC0E-1F3B-8233-17B3-AEB96AF9D030" name="AccountName" minOccurs="0" maxOccurs="1"&gt;
      &lt;xsd:annotation&gt;
        &lt;xsd:documentation&gt;&lt;/xsd:documentation&gt;
        &lt;xsd:appinfo&gt;
          &lt;label&gt;Account Name&lt;/label&gt;
          &lt;key&gt;Account Name&lt;/key&gt;
          &lt;searchable&gt;true&lt;/searchable&gt;
          &lt;timeControl&gt;false&lt;/timeControl&gt;
          &lt;description&gt;Specify the account that owns the media content&lt;/description&gt;
        &lt;/xsd:appinfo&gt;
      &lt;/xsd:annotation&gt;
      &lt;xsd:simpleType&gt;
        &lt;xsd:restriction base="listType"&gt;
          &lt;xsd:enumeration value="Analytics Business Unit Client Success Sales Eminence"/&gt;
          &lt;xsd:enumeration value="Digital"/&gt;
          &lt;xsd:enumeration value="Enterprise Media Team Enablement"/&gt;
          &lt;xsd:enumeration value="Enterprise Content and eSupport Services"/&gt;
          &lt;xsd:enumeration value="Global Delivery Center"/&gt;
          &lt;xsd:enumeration value="Digital Video Services New York"/&gt;
          &lt;xsd:enumeration value="Enterprise Social Solutions"/&gt;
          &lt;xsd:enumeration value="GBS Knowledge"/&gt;
          &lt;xsd:enumeration value="GPS Technology"/&gt;
          &lt;xsd:enumeration value="Global Private Digital Commercde"/&gt;
          &lt;xsd:enumeration value="Center"/&gt;
          &lt;xsd:enumeration value="mix"/&gt;
          &lt;xsd:enumeration value="Essentials"/&gt;
          &lt;xsd:enumeration value="References"/&gt;
          &lt;xsd:enumeration value="Learning Services"/&gt;
          &lt;xsd:enumeration value="works"/&gt;
          &lt;xsd:enumeration value="Programs &amp;amp; Operations"/&gt;
          &lt;xsd:enumeration value="IBM Digital – CHQ Marketing"/&gt;
          &lt;xsd:enumeration value="Assistant"/&gt;
          &lt;xsd:enumeration value="Content Catalog"/&gt;
          &lt;xsd:enumeration value="Center"/&gt;
          &lt;xsd:enumeration value="books"/&gt;
          &lt;xsd:enumeration value="ser"/&gt;
          &lt;xsd:enumeration value="Marketing and Communications"/&gt;
          &lt;xsd:enumeration value="Middleware Client Success"/&gt;
          &lt;xsd:enumeration value="PW"/&gt;
          &lt;xsd:enumeration value="Enablement"/&gt;
          &lt;xsd:enumeration value="Sales"/&gt;
          &lt;xsd:enumeration value="Systems Unit"/&gt;
          &lt;xsd:enumeration value="Technical"/&gt;
        &lt;/xsd:restriction&gt;
      &lt;/xsd:simpleType&gt;
    &lt;/xsd:element&gt;
    &lt;/xsd:schema&gt;
</code></pre>

<p>From the above I need to fetch only AccountName element and store all of its values as one list and further process. </p>

<p>I tried the below code which converts the String to xml. </p>

<pre><code>metadataXsd = metadataProfile.xsd;
                    if(metadataXsd != null) {
                        System.out.println("metadataXsd -----------&gt;"+metadataXsd);
                            finalXmlDoc = stringToDom(metadataXsd);
                            System.out.println("finalXmlDoc -----------&gt;"+finalXmlDoc);
                        }
                    }

public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException, TransformerException{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder;  
        try 
        {  
            builder = factory.newDocumentBuilder();  
            Document doc = builder.parse( new InputSource(new StringReader(xmlSource))); 
            TransformerFactory tranFactory = TransformerFactory.newInstance();
            Transformer aTransformer = tranFactory.newTransformer();
            Source src = new DOMSource( doc );
            Result dest = new StreamResult( new File( "C:/AccountNames.xml" ) );
            System.out.println("Destination xml --------&gt;"+dest);
            aTransformer.transform( src, dest );
            Document doc1 = builder.parse (new File("C:/AccountNames.xml")); 
            NodeList list = doc1.getElementsByTagName("xsd:element"); 

            for(int i = 0 ; i &lt; list.getLength(); i++)
            {
                Element first = (Element)list.item(i);


            }
            return doc;
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
        return null;
    }
</code></pre>

<p>Now I need to get all the   values as a Java list. Please help me resolving this.</p>