0

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?

8
  • 1
    check your all custom xsd are define while processing the xml file. link Commented Jun 15, 2022 at 5:31
  • means it's probably wrong in xsd format? @NishitCharania Commented Jun 15, 2022 at 5:47
  • looks that xml is having issue, is this possible to share your xsd and xml and stack trace ? Commented Jun 15, 2022 at 6:25
  • I have checked that the xml is valid. Have you ever had the same error as above? Commented Jun 15, 2022 at 6:58
  • From error that you have mention, I can see program not find custom type of "com/ibm/icu/text/UTF16". That is why I asked to validate your xml and asked to define xsd if needed. Hope this will help ! Commented Jun 15, 2022 at 7:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for the answer. I'm still confused, how do we know the xml or xsd is using UTF-16? Whereas in my xml it is defined as UTF-8, here is an example for xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> and for xsd : <?xml version="1.0" encoding="UTF-8"?>
Finally I found the location of the error. I've updated in the question above. It will be very helpful if there is a solution from you. Thank you
Hi @JessicaCathrine I was surprised to hear that commenting out the lines mentioned solved your problem. Although it sounds like there is a specific 'utf-16' Character in the block, I would try removing the assertion and see if you can isolate it to a smaller block of text, then you can look at the number for each char, I suspect that there is a hidden char in there somewhere
Alternatively you could pump in into a text tool such as notepad ++, show hidden chars and force and encoding change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.