开发者

Android problem with ArrayList of objects in SAX-parser

I'm trying to parse an XML page which is build like this: http://pastebin.com/t9cXdnGs. I've implemented a basic SAX parser, following this tutorial: Anddev SAX tutorial.

It worked pretty well, except I'm only getting the values of the last tag. To solve this, I've implemented an ArrayList to add each object that's made per XML node. But now I'm getting some weird output. For each node he passes in the loop, he 开发者_开发技巧adds the same value again. So for node 1, I get the value once, for node 2, I get the value twice and so on... (example: value1, value2value2, value3value3value3)

I can't seem to figure out what's wrong...

Here's my full source code of the page: http://pastebin.com/bkyz0g1U


You are re-using the same object over and over again, so you only ever add one object into your ArrayList. Replace this line:

private Vacature vacature = new Vacature();

with

private Vacature vacature;

and add this line:

vacature = new Vacature();

right after your declaration of the characters() method:

public void characters(char ch[], int start, int length) {

I also suspect that getVacatures() should return the ArrayList arrayVaca.


I quickly changed your code to a simpler solution:

    package stage.accent.webservice;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import stage.accent.domain.Vacature;

public class vacatureWebservice extends DefaultHandler {



        private ArrayList<Vacature>arrayVaca = new ArrayList<Vacature>();

        private Vacature vacature = null;

        public ArrayList<Vacature> getVacatures() {
            return this.arrayVaca;

        }

        public StringBuilder buff =  new StringBuilder();

        @Override
        public void startDocument() throws SAXException {

            this.vacature = new Vacature();
        }

        @Override
        public void endDocument() throws SAXException {
            // Nothing to do

        }

        /** Gets be called on opening tags like: 
         * <tag> 
         * Can provide attribute(s), when xml was like:
         * <tag attribute="attributeValue">*/
        @Override
        public void startElement(String namespaceURI, String localName,
                String qName, Attributes atts) throws SAXException {

        }

        /** Gets be called on closing tags like: 
         * </tag> */
        @Override
        public void endElement(String namespaceURI, String localName, String qName)
                throws SAXException {
            if (localName.equals("vacatures")) {

            }else if (localName.equals("vacature")) {
            }else if (localName.equals("titel")) {
                vacature.setTitel(buff.toString());         
            }else if (localName.equals("werkveld")){            
                vacature.setWerkveld(buff.toString());
            }else if (localName.equals("regio")){

                vacature.setRegio(buff.toString());
                arrayVaca.add(vacature);
                vacature = new Vacature();

            }

            buff = new StringBuilder();
        }

        /** Gets be called on the following structure: 
         * <tag>characters</tag> */
        @Override
        public void characters(char ch[], int start, int length) {          
             if (buff!=null) {
                    for (int i=start; i<start+length; i++) {
                        buff.append(ch[i]);
                    }
                }

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜