Good way to parse XML like this sample?

_Fachcaws

New Member
I am wondering what's the best practice to parse XML like this:\[code\]<root> <MailNotification enable="true"> <To>[email protected]</To> <From>[email protected]</From> <Server>smtp.bar.org</Server> <Port>465</Port> <Username>[email protected]</Username> <Password>fooo!</Password> </MailNotification></root>\[/code\]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!
 
Back
Top