用于解析 XSD 模式文件的 Java API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3996857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:07:22  来源:igfitidea点击:

Java API to parse XSD schema file

javaxmlxsd

提问by Henryk Konsek

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

是否有用于解析 XSD 架构文件的 Java API?

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

我找到了XSOM,但它似乎不再维护了。

采纳答案by emicklei

Using standard JDK 6:

使用标准 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");

回答by Chris Aldrich

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

我们倾向于使用 Apache Xerces http://xerces.apache.org/。对我们来说效果很好。

回答by Qwerky

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.validationpackage.

如前所述,模式本身就是有效的 XML。你想加载它做什么,验证?如果是这样,那么包中有一些方便的类javax.xml.validation

回答by Marcus

JAXB

JAXB

See this question

看到这个问题

回答by Andrew Harmel-Law

Depends what your specific requirements are. This pagegives 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.

需要考虑的一件事:根据您要部署到的平台和您使用的其他框架,您的依赖树中可能已经有各种 API(例如,xerces 在 App Servers 中特别常见)。注意版本冲突,这可能是隔离和调试的噩梦。

回答by Martin

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

试试EasyWSDL- 它可以解析 XSD 和 WSDL 规范。

回答by Chuck

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.

您可以使用 XMLBeans http://xmlbeans.apache.org。对于围绕 XML/Java 绑定的大多数用例,人们似乎正在转向 JAXB,因为它是内置的。但是对于您想要做的事情,XMLBeans 提供了对一些以易于理解的方式表示模式的高级数据结构的访问遍历。

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.

从那里您应该能够弄清楚如何从 JavaDoc 深入了解 SchemaTypeSystem。

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.

如果有人知道一种既方便又相对受支持的替代方案,那么了解它会很棒。我正在积极寻找。

回答by Roshan Dsouza

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