2013-06-15 3 views
0

XML 파일을 메모리로 구문 분석하려고하므로 목록 뷰로 확장 할 수 있습니다. 문제는 내가 마지막으로 등록한 것만 유지할 수 있다는 것입니다.Sax XML을 구문 분석하지만 마지막 레지스터 만 메모리에 유지됩니다.

나는 뭔가를 놓치고 있다고 생각하지만 나는 그것이 무엇인지 모른다. 나는 흐르는 레지스터를 모두 볼 수있는 로그 캣에서

... 도움을

감사합니다!

활동 파일 :

try { 

     /** 
     * Create a new instance of the SAX parser 
     **/ 
     SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
     SAXParser saxP = saxPF.newSAXParser(); 
     XMLReader xmlR = saxP.getXMLReader(); 

     /** 
     * Create the Handler to handle each of the XML tags. 
     **/ 
     XMLHandler myXMLHandler = new XMLHandler(); 
     xmlR.setContentHandler(myXMLHandler); 
     xmlR.parse(new InputSource(getAssets().open("company_details.xml"))); 

    } catch (Exception e) { 
     System.out.println(e); 
    } 

    data = XMLHandler.data; 

XMLHandler :

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

import android.app.Application; 

public class XMLHandler extends DefaultHandler { 

String elementValue = null; 
Boolean elementOn = false; 

public static XMLGettersSetters data = null; 

public static XMLGettersSetters getXMLData() { 
    return data; 
} 

public static void setXMLData(XMLGettersSetters data) { 
    XMLHandler.data = data; 
} 

/** 
* This will be called when the tags of the XML starts. 
**/ 
@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 

    elementOn = true; 

    if (localName.equals("todo")) 
    { 
     data = new XMLGettersSetters(); 
    } else if (localName.equals("CD")) { 
     /** 
     * We can get the values of attributes for eg. if the CD tag had an attribute(<CD attr= "band">Akon</CD>) 
     * we can get the value "band". Below is an example of how to achieve this. 
     * 
     * String attributeValue = attributes.getValue("attr"); 
     * data.setAttribute(attributeValue); 
     * 
     * */ 
    } 
} 

/** 
* This will be called when the tags of the XML end. 
**/ 
@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 

    elementOn = false; 

    /** 
    * Sets the values after retrieving the values from the XML tags 
    * */ 
    if (localName.equalsIgnoreCase("day")) 
     data.setDia(elementValue); 
    else if (localName.equalsIgnoreCase("week_day")) 
     data.setDiaSemana(elementValue); 
    else if (localName.equalsIgnoreCase("text")) 
     data.setTexto(elementValue); 
} 

/** 
* This is called to get the tags value 
**/ 
@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 

    if (elementOn) { 
     elementValue = new String(ch, start, length); 
     elementOn = false; 
    } 

} 

}

XMLGettersSetters :

import java.util.ArrayList; 

import android.util.Log; 

/** 
* This class contains all getter and setter methods to set and retrieve data. 
* 
**/ 
public class XMLGettersSetters { 

private ArrayList<String> day = new ArrayList<String>(); 
private ArrayList<String> weekday = new ArrayList<String>(); 
private ArrayList<String> text = new ArrayList<String>(); 

public ArrayList<String> getDay() { 
    return day; 
} 

public void setDay(String dia) { 
    this.day.add(day); 
    Log.i("This is the company:", day); 
} 

public ArrayList<String> getWeekDay() { 
    return weekday; 
} 

public void setWeekDay(String weekday) { 
    this.weekday.add(weekday); 
    Log.i("This is the year:", weekday); 
} 

public ArrayList<String> getText() { 
    return texto; 
} 

public void setText(String texto) { 
    this.texto.add(texto); 
    Log.i("This is the year:", text); 
} 

}

+0

에서 인스턴스화 : 때마다 당신은 내부에 태그 startElement 당신이 override 데이터의 현재 가치를 얻을? – Blackbelt

답변

1

해당 문제는 todo 태그와 관련이 있습니다. 그것은 startElement에서 제거하고 태그 "할 일이"현재 얼마나 많은 시간 선언 시간으로 XML 내부

public static XMLGettersSetters data = new XMLGettersSetters(); 
+0

바로 그 자리에! 감사 블랙 벨트! – RickON