开发者

Java: Is there any possibility to check a String against a SimpleType from a XML schema with Java?

i have a really complex XML-Schema (it is designed by Swift :-) There are many simpleTypes declared which will be used within the Schema-Validation.

Now in my Java-Web-Application I have some input fields which should have the same validation rules like described in the XML-Schema. For every input field I know the corresponding SimpleType from the schema. So is there any possibility to check the开发者_运维百科 String I get from the input field against the rules defined in the schema?

I don't want to generate an Java-Object-Model from the schema with Jaxb because the schema is really complex and the genreation leads to nearly 100 Java-classes only for one schema definition.


I do not know of any Java validation model automatically generated from XSD simple types. Even with JAXB or XMLBeans restrictions such as XSD patterns do not apply to generated Java code (you see getters/setters for play String objects).

As you do not want to use a JAXB model you could just use for example XSD validation of the XML processor. So to check the values you need to create a valid (minimal) XML and process it by means of a XML processsor supporting XSD validation (Xerces DOM will be ok). If validation passes you know input data is correct.


You can potentially leverage javax.xml.validation API and validate small XML snippets that you produce in memory and that hold your simple types.

You can find some API usage examples here. Instead of loading XML source file from disk you will stream it from e.g. ByteArrayInputStream.

Maybe you will need to generate another schema that will depend on XSD from Swift to have an element for each simple type to make generation of XML snippets easier.

EDIT: Another possiblity is to use one of the Java libraries that work with SWIFT. Some overview is given in this question: Java SWIFT Library

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜