Amazon Interview Question
Software Engineer / Developers1.Use regex like (<([a-zA-Z])+(\s)*) to find XML tags , now we can have one such regex for each type of tag like self ending tags (<([a-zA-Z])+(\s)* />), regex accounting for attritubes, comments etc.
2. Run through the input file, to find such patterns, and then create either a DOM like tree or any datas tructure to hold the XML structure.
3. A SAX parser also goes through the input file sequentially, (so does DOM parser), but the only difference is SAX is event based, so sequential parsing is justified.
4. Now we can use a Observer pattern to to let the subscriber(s) know that an event has occurred when we encounter a match.
An alternative way is to do it the crude way, hunt for a < and then look for all cases that it could match (could be an element tag, could be a comment, element could have attributes) and so on. But I feel the previous method is more robust and lets us handle more number of cases in less amount of trouble.
Use SAX parser
- iPondy June 11, 2010