Java API to parse XSD schema file [closed]

Is there a Java API to parse an XSD schema file?

I found XSOM, but it doesn't seem to be maintained anymore.


Solution 1:

Using standard JDK 6:

System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 
com.sun.org.apache.xerces.internal.impl.xs.XSImplementationImpl impl = (XSImplementationImpl) registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
XSModel model = schemaLoader.loadURI("src/test/resources/my.xsd");

Solution 2:

public class XsdElements {
    public static void main(String args[]) { 
        try { 
            // parse the document
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("C:/Users/rb054/Desktop/Descriptor.1.5.1.xsd")); 
            NodeList list = doc.getElementsByTagName("xs:element"); 

            //loop to print data
            for(int i = 0 ; i < list.getLength(); i++)
            {
                Element first = (Element)list.item(i);
                if(first.hasAttributes())
                {
                    String nm = first.getAttribute("name"); 
                    System.out.println(nm); 
                    String nm1 = first.getAttribute("type"); 
                    System.out.println(nm1); 
                }
            }
        } 
        catch (ParserConfigurationException e) 
        {
            e.printStackTrace();
        }
        catch (SAXException e) 
        { 
            e.printStackTrace();
        }
        catch (IOException ed) 
        {
            ed.printStackTrace();
        }
    }
}