Search code examples
androidtagssaxsaxparser

Android SAX Parser - Same tag names on several levels


I've been looking for a solution in order to parse my xml that has same tag names on several levels. Here is a XML sample of what I have to work on (the sections are not static):

<xml>
  <section id="0">
     <title>foo</title>
     <section id="1">
        <title>sub foo #1</title>
        <section id="2">
          <title>sub sub foo</title>
        </section>
     </section>
     <section id="3">
        <title>sub foo #2</title>
     </section>
  </section>
<xml>

I have been trying several possibilities, such as trying Lists, Stacks, but what I have done with SAX hasn't produced anything correct yet; in other words I'm stuck :(

I created a class called Section:

public class Section {
public String id;
public String title;
public List<Section> sections; }

I'm wondering if I should also add a parent variable?

public Section parent;

If anyone has a solution, I thank you very much! :D


Solution

  • Indeed you likely need at least a stack for this.

    With (I hope) clear changes to your Section class (setters/getters and a method to add a section), this handler seems to do the trick:

    Since your layout appears to allow for multiple <section> tags immediately below the <xml> root, I've implemented it also putting the results into a List<Section>.

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    public class SectionXmlHandler extends DefaultHandler {
    
        private List<Section> results;
        private Stack<Section> stack;
        private StringBuffer buffer = new StringBuffer();
    
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if ("xml".equals(localName)) {
                results = new ArrayList<Section>();
                stack = new Stack<Section>();
            } else if ("section".equals(localName)) {
                Section currentSection = new Section();
                currentSection.setId(attributes.getValue("id"));
                stack.push(currentSection);
            } else if ("title".equals(localName)) {
                buffer.setLength(0);
            }
        }
    
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("section".equals(localName)) {
                Section currentSection = stack.pop();
                if (stack.isEmpty()) {
                    results.add(currentSection);
                } else {
                    Section parent = stack.peek();
                    parent.addSection(currentSection);
                }
            } else if ("title".equals(localName)) {
                Section currentSection = stack.peek();
                currentSection.setTitle(buffer.toString());
            }
        }
    
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            buffer.append(ch, start, length);
        }
    
        public List<Section> getResults() {
            return results;
        }
    }