The question is:

Implement XML/HTML Parser: essentially the same as the input is a tokenizer object, you can call its getNextToken() function to get the next token, token structure including name and tag, name is a specific text, tag have open, close, text three kinds, let all the token to build a tree for example:

<html>
    <user>
        <id>aa</id>
        <meta>bb</meta>
    </user>
</html>

Tokens:

("html","open") ("user","open") ("id","open") ("aa","text") ("id","close") 
("meta","open") ("bb","text") ("meta","close") ("user","close") ("html","close")

Implemented the following but am I understanding and answering the question correctly? And how can I implement it in Java?

class Node(object):
    def __init__(self):
        self.text = None
        self.children = {}

class Solution(object):
    def xmlParser(self, tokens):
        root = Node()
        s = [root]
        for t in tokens:
            print t
            if t[1]=='open':
                tmp = Node()
                s[-1].children[t[0]] = tmp
                s.append(tmp)
            elif t[1]=='close':
                s.pop()
            elif t[1]=='text':
                s[-1].text = t[0]
        return root

Thank you in advance and will accept/upvote the answer

share|improve this question
1  
Stack Overflow is not a code writing service. You are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal, Complete, and Verifiable example. I suggest reading how to ask a good question. – John Conde 46 mins ago
    
@JohnConde Don't think he is asking to write the whole code. It's already written. The question seem valid to me. – Ly Maneug 44 mins ago
    
Asking how to write it in Java is asking for the code. Additionally, can't they run this script and verify the output themselves? – John Conde 38 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.