开发者

Java API to parse XSD schema file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. 开发者_开发百科

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 6 years ago.

Improve this question

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

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


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");


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();
        }
    }
}


We have tended to use Apache Xerces http://xerces.apache.org/. Works really well for us.


You can use XMLBeans http://xmlbeans.apache.org. For most use cases around XML/Java binding people seem to be moving to JAXB since it is built in. But for what you want to do, XMLBeans provides access to some great high level data structures that represent schemae in a way that is easy to traverse.

You can start with something like...

SchemaTypeSystem sts = XmlBeans.compileXsd(new XmlObject[] {
    XmlObject.Factory.parse(xsdFile) }, XmlBeans.getBuiltinTypeSystem(), null);

From there you should be able to figure out how to drill into the SchemaTypeSystem from the JavaDoc.

If someone knows of an alternative that is as convenient and relatively supported, it would be great to know about it. I'm actively looking.


Try EasyWSDL - it can parse both XSD an WSDL specifications.


JAXB

See this question


As mentioned, a schema is itself valid XML. What do you want to load it for, validation? If so then there are some handy classes in the javax.xml.validation package.


Depends what your specific requirements are. This page gives a good overview of the major options and the use cases they support.

One thing to think about: Depending on the platform you are deploying to and other frameworks you are using, there may already be various APIs in your dependency tree (xerces is particularly common in App Servers for example). Watch out for version conflicts which can be a nightmare to isolate and debug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜