Home > OS >  error when validation xml with xsd spring boot
error when validation xml with xsd spring boot

Time:06-17

I want to validate the generated xml, with the xsd file. But I got the following error message.

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/ibm/icu/text/UTF16

Here's my code:

public void validateAgainstXSD(File xml) {
        try {
            SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
            Schema schema = factory.newSchema(new File(xsdFilepath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(xml));
            System.out.println("success");
        } catch (Exception ex) {
            ex.getMessage();
        }
    }

And the error message is in the following line:

validator.validate(new StreamSource(xml));

Does anyone know why that happened? And what is the solution for successful validation? I really need your help, thank you.

Finally I found where the error lies. The error is in the following XSD:

<xs:simpleType name="nameNType">
    <xs:restriction base="xs:string">
        <xs:assertion test="string-length($value) &lt;= 65"/>
    </xs:restriction>
</xs:simpleType>

If I comment on the code snippet above, it has been successfully validated and no error message appears as above.

Another question, what is the format so that I can still use the xsd code snippet? How to change it to UTF-8?

CodePudding user response:

Being unable to find the class com/ibm/icu/text/UTF16 indicates 2 things:

  1. You don't have that class on your class-path.
  2. The parser is trying to parse thinking that either the xsd or XML are using the UTF-16 character set.

So this leaves a few solutions

To fix 1 you could, find a jar with the character set class, and include it on your class-path. This may work but it would not be my first choice.

To fix 2 you will need to understand why it is trying to use uft-16. If this is not correct, you may be able to specify another set when reading your files. Additionally you may want to investigate where the class com/ibm/icu/text/UTF16 is being specified, and possibly specify a different character set class.

Typically I ensure that all files I load are in utf-8, specify utf-8 during read, and have it specified at a system level.

  • Related