Siva Kumar
Siva Kumar

Reputation: 893

Parsing an XML dynamically in Android?

Is there any way of parsing an XML file without knowing the tags present in the XML file ?

An XML file is given as input and it has to be parsed .. As we do not know the tags present in it , I found it difficult to parse using either DOM Parser or SAX parser.

Is there any way of getting this ??

Thanks, Siva Kumar

Upvotes: 0

Views: 2544

Answers (3)

noname
noname

Reputation: 423

you can get your XML file in asset folder and parse the XML file like this:

    NodeList root = doc.getElementsByTagName("root");
    NodeList nlQuestions = root.item(0).getChildNodes();

    QuestionObject[] allQuestions = new QuestionObject[nlQuestions.getLength()];

    for (int i = 0; i < nlQuestions.getLength(); i++){
        Node question =  nlQuestions.item(i);
        NodeList childNodes = question.getChildNodes();

        QuestionObject x = new QuestionObject();

        for (int j = 0; j < childNodes.getLength(); j++){
            Node child =  childNodes.item(j);               

            if (child.getNodeName() !="#text"){
                Questions t = Questions.valueOf(child.getNodeName());

Upvotes: 0

Bert F
Bert F

Reputation: 87533

I'm unclear what the issue is. The Java DOM and SAX parsers don't require the element tags and attributes to be known in advance. It will parse any well-formed XML. I'm not familiar with XML parsing on Android specifically, but tutorial here doesn't seem to indicate the parsers need to know the element tags and attributes in advance:

http://www.ibm.com/developerworks/opensource/library/x-android/

For example, you can use DOM to parse an XML document to get a DOM tree. Since you don't know the element names, you can't just query the DOM tree for a specific child or descendant element by element name, but you can walk the tree through calls like Document.getDocumentElement() and Node.getChildNodes(). Its up to your code to know what to do with that structured DOM tree that results from parsing the XML document, but the DOM parser itself doesn't care about what the specific element tags and attributes are.

Similarly you can feed an XML document to a SAX parser and SAX will parse through it fine. SAX will call your ContentHandler.startElement() callback with the element name and a map of attributes in that element. Your code needs to determine what to do with the elements and attributes that the SAX parser finds as it processes the XML document, but SAX doesn't care what the specific element tags and attributes are.

So the question comes down to, what do you want to your code to do with the results of parsing the XML? Are you trying to identify it? Are you trying to pretty-print it? Are you trying to convert it to another format?

Upvotes: 5

Rohit Sharma
Rohit Sharma

Reputation: 13815

For this you have to read it as a simple file. Say by using BufferedReader. Then while reading each line search for opening and closing tab or pattern matching for <> or </>.

and read the rest character and fill it in your own buffers. in short it is like developing your own raw parser.

but it will work for sure. though its tedious .:)

Upvotes: 1

Related Questions