1. Nimm jetzt an unserem 2. ADVENT-Gewinnspiel teil - Alle Informationen findest Du hier!

Probleme beim lesen einer XML Datei

Dieses Thema im Forum "Android App Entwicklung" wurde erstellt von Raidri, 03.09.2011.

  1. Raidri, 03.09.2011 #1
    Raidri

    Raidri Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    71
    Erhaltene Danke:
    1
    Registriert seit:
    27.03.2010
    Hallo,

    ich möchte, wie unten zu sehen, eine XML Datei auslesen. Dafür nutze ich den SAXParser und den DefaultHandler. Er liest einige der Daten aus aber nicht alle. Zum Beispiel bekomme ich nie das Feld "locked" ausgelesen, weil er nicht einmal in den Knoten reingeht, obwohl im Defaulthandler "locked" gefunden wurde. Vllt könnt ihr mir da weiterhelfen. Unten führe ich die nötigen Dateien auf.

    Code:
    <time-entries type="array">
    <time-entry>
    <id type="integer">8295580</id>
    <date-at type="date">2011-09-03</date-at>
    <minutes type="integer">120</minutes>
    <revenue type="float" nil="true"/>
    <project-id type="integer">491108</project-id>
    <locked type="boolean">true</locked>
    <service-id type="integer">160608</service-id>
    <user-id type="integer">39342</user-id>
    <created-at type="datetime">2011-09-02T20:05:50+02:00</created-at>
    <updated-at type="datetime">2011-09-03T13:21:33+02:00</updated-at>
    <billable type="boolean">false</billable>
    <note/>
    <user-name>Pascal geldmacher</user-name>
    <customer-id type="integer">137475</customer-id>
    <customer-name>deltra</customer-name>
    <project-name>Projekt #5</project-name>
    <service-name>Support</service-name>
    </time-entry>
    </time-entries>
    
    Erzeugen des SAXParser und des Handlers
    Code:
    InputStream is = entity.getContent();
    						
    						
    						SAXParserFactory spf = SAXParserFactory.newInstance();
    						SAXParser sp = spf.newSAXParser();
    						
    						XMLReader reader = sp.getXMLReader();
    						
    						TimeXMLHandler handler = new TimeXMLHandler();
    
    						reader.setContentHandler(handler);
    						
    						reader.parse(new InputSource(is));
    					    
    						is.close();
    
    Und nun der Handler, wo er nie in den locked Teil geht.
    Code:
    @SuppressWarnings("unused")
    public class TimeXMLHandler extends DefaultHandler {
    	
    	private boolean time_entries = false;
    	private boolean time_entry = false;
    	private boolean id = false;
    	private boolean dateAt = false;
    	private boolean minutes = false;
    	private boolean billable = false;
    	private boolean note = false;
    	private boolean project_id = false;
    	private boolean project_name = false;
    	private boolean service_id = false;
    	private boolean service_name = false;
    	private boolean customer_id = false;
    	private boolean customer_name = false;
    	private boolean locked = false;
    	private boolean createdAt = false;
    	private boolean tracking = false;
    	private boolean trackingMinutes = false;
    	
    	private String noted = "";
    	private String projName = "";
    	private String custName = "";
    	private String servName = "";
    	
    	private ArrayList<TimeEntry> list = new ArrayList<TimeEntry>();
    	private TimeEntry entry = new TimeEntry();
    	
    	public TimeXMLHandler(){
    		
    	}
    	
    	public ArrayList<TimeEntry> getTimeEntry(){
    		return this.list;
    	}
    	
    	 @Override
         public void startDocument() throws SAXException {
         }
    	 
    	 @Override
         public void endDocument() throws SAXException {
              // Nothing to do
         }
    	 
    	 @Override
         public void startElement(String namespaceURI, String localName,
                   String qName, Attributes atts) throws SAXException {
             if(localName.equals("time-entries")){
            	 this.time_entries = true;
             }
             else if(localName.equals("time-entry")){
            	 this.entry = new TimeEntry();
            	 this.time_entry = true;
             }
             else if(localName.equals("id")){
            	 this.id = true;
             }
             else if(localName.equals("date-at")){
            	 this.dateAt = true;
             }
             else if(localName.equals("minutes")){
            	 if(this.tracking){
            		 this.trackingMinutes = true;
            		 this.entry.setTrackerID(1);
            	 }
            	 else{
            		 this.minutes = true;
            	 }
             }
             else if(localName.equals("billable")){
            	 this.billable = true;
             }
             else if(localName.equals("locked")){
            	 this.locked = true;
             }
             else if(localName.equals("customer-id")){
            	 this.customer_id = true;
             }
             else if(localName.equals("customer-name")){
            	 this.custName = "";
            	 this.customer_name = true;
             }
             else if(localName.equals("project-id")){
            	 this.project_id = true;
             }
             else if(localName.equals("project-name")){
            	 this.projName = "";
            	 this.project_name = true;
             }
             else if(localName.equals("service-id")){
            	 this.service_id = true;
             }
             else if(localName.equals("service-name")){
            	 this.servName = "";
            	 this.service_name = true;
             }
             else if(localName.equals("created-at")){
            	 this.createdAt = true;
             }
             else if(localName.equals("note")){
            	 this.noted = "";
            	 this.note = true;
             }
             else if(localName.equals("tracking")){
            	 this.tracking = true;
             }
         } 
    	 
    	 @Override
         public void endElement(String namespaceURI, String localName, String qName)
                   throws SAXException {
    		  	 if(localName.equals("time-entries")){
    	        	 this.time_entries = false;
    	         }
    	         else if(localName.equals("time-entry")){
    	        	 this.list.add(this.entry);
    	        	 this.time_entry = false;
    	         }
    	         else if(localName.equals("id")){
    	        	 this.id = false;
    	         }
    	         else if(localName.equals("date-at")){
    	        	 this.dateAt = false;
    	         }
    	         else if(localName.equals("minutes")){
    	        	 if(this.tracking){
    	        		 this.trackingMinutes = false;
    	        	 }
    	        	 else{
    	        		 this.minutes = false;
    	        	 }
    	         }
    	         else if(localName.equals("billable")){
    	        	 this.billable = false;
    	         }
    	         else if(localName.equals("locked")){
    	        	 this.locked = false;
    		     }
    	         else if(localName.equals("customer-id")){
    	        	 this.customer_id = false;
    	         }
    	         else if(localName.equals("customer-name")){
    	        	 this.entry.setCname(custName);
    	        	 this.customer_name = false;
    	         }
    	         else if(localName.equals("project-id")){
    	        	 this.project_id = false;
    	         }
    	         else if(localName.equals("project-name")){
    	        	 this.entry.setpName(projName);
    	        	 this.project_name = false;
    	         }
    	         else if(localName.equals("service-id")){
    	        	 this.service_id = false;
    	         }
    	         else if(localName.equals("service-name")){
    	        	 this.entry.setsName(servName);
    	        	 this.service_name = false;
    	         }
    	         else if(localName.equals("created-at")){
    	        	 this.createdAt = false;
    	         }
    	         else if(localName.equals("note")){
    	        	 this.entry.setNote(noted);
    	        	 this.note = false;
    	         }
    	         else if(localName.equals("tracking")){
    	        	 this.tracking = false;
    	         }
         } 
    	 
    	 @Override
    	 public void characters(char ch[], int start, int length) {
    	      if(this.id){
    	    	  this.entry.setId(Integer.parseInt(new String(ch, start, length)));
    	      }
    	      else if(this.dateAt){
    	    	  this.entry.setDateAt(new String(ch, start, length));
    	      }
    	      else if(this.billable){
    	    	  this.entry.setBillable(Boolean.parseBoolean(new String(ch, start, length)));
    	      }
    	      else if(this.locked){
    	    	  this.entry.setLocked(Boolean.parseBoolean(new String(ch, start, length)));
    	      }
    	      else if(this.createdAt){
    	    	  this.entry.setCreatedAt(new String(ch, start, length));
    	      }
    	      else if(this.project_id){
    	    	  if(length > 0){
    	    		  this.entry.setPid(Integer.parseInt(new String(ch, start, length)));
    	    	  }
    	      }
    	      else if(this.project_name){
    	    	  if(length > 0){
    	    		  this.projName += new String(ch, start, length);
    	    	  }
    	      }
    	      else if(this.note){
    	    	  this.noted = new String(ch, start, length);
    	      }
    	      else if(this.customer_id){
    	    	  if(length > 0){
    	    		  this.entry.setCid(Integer.parseInt(new String(ch,start, length)));
    	    	  }
    	      }
    	      else if(this.customer_name){
    	    	  if(length > 0){
    	    		  this.custName = new String(ch, start, length);
    	    	  }
    	      }
    	      else if(this.service_id){
    	    	  if(length > 0){
    	    		  this.entry.setSid(Integer.parseInt(new String(ch, start, length)));
    	    	  }
    	      }
    	      else if(this.service_name){
    	    	  if(length > 0){
    	    		  this.servName = new String(ch, start, length);
    	    	  }
    	      }
    	      else if(this.minutes || this.trackingMinutes){
    	    	 this.entry.setMinutes(Integer.parseInt(new String(ch, start, length)));
    	    	
    	      }
    	 } 
    	 
    	 
    }
    
    Vielen Dank für die Hilfe
     
  2. ko5tik, 03.09.2011 #2
    ko5tik

    ko5tik Android-Experte

    Beiträge:
    620
    Erhaltene Danke:
    40
    Registriert seit:
    14.10.2009
    Phone:
    HTC Hero
    Damit es so funktioniert, wie du es dir gedacht hast, must due deine booleans alle auf false setzen,
    wenn du einen neuen element anfängst (im startElement()).

    Eine einfachere und performantere Alternative wäre ein Enum, und anscließend switch
     
  3. sixi, 03.09.2011 #3
    sixi

    sixi Erfahrener Benutzer

    Beiträge:
    237
    Erhaltene Danke:
    64
    Registriert seit:
    20.01.2009
    Fürs XML Parsen bietet sich ja die neue Java7 switch(String) Anweisung fast an :)
     

Diese Seite empfehlen