my Problem is that i have to parse a xml-document that can have two different root xml-tags.
Variant 1:
<?xml version="1.0" encoding="UTF-8"?>
<success>
success...
</success>
Variant 2:
<?xml version="1.0" encoding="UTF-8"?>
<error>
error...
</error>
I know there is the annotation @ElementUnion()
, but as far as i understand it only works for non-root tags.
Some more information:
I'm working with retrofit and consume an api and use simple-xml to parse the xml-document.
It would be great if someone has a solution or a workaround.
The only idea i have is to call the api, catch the exception and retry with the other model for variant 2.
Thanks in advance
You can use the Persister.validate
. It takes the same arguments as the Persister.read
and returns simple true or false if the passed class can be parsed.
This is the best possible way using the API, you could also manually detect the class by reading the first two lines of the xml (in your example) but in more complicated structures you need to use the 'validate'.