3

I have something like this:

Java Class

.....
@XStreamAlias("SOME_TAG")
    private String someAttribute;
.....

<ROOT>
    <ANOTHER_TAG>VALUE</ANOTHER_TAG>
</ROOT>

And in my xml i need to have this "SOME_TAG", if for some reason it is missing i need to throw an exception.

Can i do it with XStream?

Regards

John
  • 41
  • 1
  • 4

2 Answers2

1

I'm not a experimented user of xstream, but I would do this:

  1. Make a dtd file or xsd file, where you can specify the required elements, then you can validate the xml file against the dtd or xsd file. Or,
  2. Make a validation method inside the class for validate is any field is null or any other validation you need to do.
nashuald
  • 805
  • 3
  • 14
  • 31
1

Purely using XStream I think you have two options:

  1. XStream uses the same mechanism as the JDK serialization, so you can simply implement a Object readResolve() method in your class that is being deserialized. This method is called after the object has been initialized and had all fields set by XStream, so you can use this method to check if your someAttribute field is null - if it is you can then throw an exception. (Examples from XStream can be found here: http://x-stream.github.io/faq.html#Serialization_initialize_transient

  2. Another alternative would be to write a custom Converter for your class and check there if the field is filled or not.

facundofarias
  • 2,973
  • 28
  • 27
DB5
  • 13,553
  • 7
  • 66
  • 71