0

I need to validate the structure of an XML in input using JSR-303 annotations. So I want my XML to contain all the required tags, and I accept if the XML tags contain no value at all. So there must be all the tags and they can be empty.

The problem is that if @NotNull annotation works well for Strings (so I can have the xml tag without value in it), for Double variables I cannot use it, because if I empty the tag, it returns me the error like I delete the tag also.

For istance:

<extTag>
  <myString>Hello</myString>   -> @NotNull String myString; -> OK!
</extTag>

<extTag>
  <myString></myString>        -> @NotNull String myString; -> OK!
</extTag>

<extTag>
                               -> @NotNull String myString; -> ERROR!
</extTag>

For Double I would like to have the same control, but when the value is null I get the error!

<extTag>
  <myDouble>12.00</myDouble>   -> @NotNull Double myDouble; -> OK!
</extTag>

<extTag>
  <myDouble></myDouble>        -> @NotNull Double myDouble; -> ERROR!
</extTag>

<extTag>
                               -> @NotNull Double myDouble; -> ERROR!
</extTag>

I can I get the same behaviour od strings for Double variables using annotations?

Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62

1 Answers1

1

Maybe you should use a diferent technique to validate XML structure, use a XML Schema Definition (XSD) or iterate the hole document with a parser.

I think you are a little confused with XML and Bean Validation, remember XML stores a String representation of the Bean values, and that JSR-303 is about value validation:

  • For String you have an empty value representation and this: <myString></myString> is just fine because is equivalent to this "".

  • For Double there is no empty representation, so this <myDouble></myDouble> is a null value.

Rafael Guillen
  • 1,343
  • 10
  • 25