I am wondering what's the best practice to parse XML like this:
<root>
<MailNotification enable="true">
<To>foo@bar.org</To>
<From>foo@bar.org</From>
<Server>smtp.bar.org</Server>
<Port>465</Port>
<Username>foo@bar.org</Username>
<Password>fooo!</Password>
</MailNotification>
</root>
I am using Java 7, the complete XML is longer, but it's no really big file. I thought about using a Stax Pull Parser because it seemed easy, but there's one thing where I am not sure if it is really a good way:
When coming to a MailNotification element, I could e.g. create a new instance of e.g. a mail class, I have no problem with that. But: What if I come e.g. to an To element? How do I know if it is really inside a MailNotification element and not directly below the root? In other words: What I am missing is a best practice for handling states like "now I am in a MailNotification" element.
Note: I know I could verify the XML first, but imagine it would be allowed to have a To element inside a MailNotification element and a To element as children of another, semantically different element - same problem: I somehow need to keep track of states / context to make sure I interpret the To element correctly.
Thanks for any hint!