Umlaute aus XML, HTML-Darstellung in App

  • 6 Antworten
  • Letztes Antwortdatum
W

whitebrazilian

Neues Mitglied
0
Hallo zusammen,

ich lese in meiner App eine XML-Datei aus dem Internet mit Hilfe des SaxParsers aus und lege hierbei Objekte an, die im Anschluss in einer ListView angezeigt werden.

Leider werden die Umlaute in der ListView entsprechend ihres HTML-Wertes (z.B. ü ) anstelle der deutschen Schreibweise (z.B. ü) ausgegeben.

Das richtige encoding beim Auslesen der XML-Datei habe ich gesetzt. Muss ich bei der Ausgabe auch etwas beachten?

Hier noch ein paar Code-Schnipsel:
Code:
/* Get a SAXParser from the SAXPArserFactory. */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();

                /* Get the XMLReader of the SAXParser we created. */
                XMLReader xr = sp.getXMLReader();
                /* Create a new ContentHandler and apply it to the XML-Reader */
                FddbXmlHandler fddbXmlHandler = new FddbXmlHandler();

                xr.setContentHandler(fddbXmlHandler);

                InputSource is = new InputSource();
                is.setEncoding("ISO-8859-1");
                is.setByteStream(url.openStream());
                /* Parse the xml-data from our URL. */
                xr.parse(is);
                /* Parsing has finished. */

                /* Our Handler now provides the parsed data to us. */
                return fddbXmlHandler.getParsedData();
Hier mein Handler:
Code:
package com.healthapp.foodinterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


public class FddbXmlHandler extends DefaultHandler {
    
    // ===========================================================
        // Fields
        // ===========================================================

        private boolean in_item = false;
        private boolean in_id = false;
        private boolean in_productcode_ean = false;

        private boolean in_data = false;
        private boolean in_amount = false; // the amount of the calculation base,
                                            // e.g. 100
        private boolean in_amount_measuring_system = false; // the unit of the
                                                            // calculation base,
                                                            // e.g. Mass (gram)
        private boolean in_kcal = false;
        private boolean in_protein_gram = false;
        private boolean in_kh_gram = false;
        private boolean in_fat_gram = false;

        private boolean in_servings = false;
        private boolean in_serving = false;
        private boolean in_serving_name = false; // Tag = <name></name>
        private boolean in_serving_amount = false; // Tag =
                                                    // <weight_gram></weight_gram>

        private boolean in_description = false;
        private boolean in_fooditem_name = false; // Tag = <name></name>
        private boolean in_fooditem_producer = false;
        private boolean in_fooditem_option = false;

        private FoodItem foodItem = null;
        private HashMap<String, Double> servings = null;
        
        private List<FoodItem> foodItems;
        
        private StringBuilder textContent = new StringBuilder();
        private String text;
        
        private double serving_amount;
        
        private String serving_name = ""; // helper for saving the name of the current serving until it is put into the servings map

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        public List<FoodItem> getParsedData() {
            return this.foodItems;
        }

        // ===========================================================
        // Methods
        // ===========================================================
        @Override
        public void startDocument() throws SAXException {
            this.foodItems = new ArrayList<FoodItem>();
        }

        @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 {
            
            textContent.setLength(0);
            
            if (localName.equals("item")) {
                this.in_item = true;
                foodItem = new FoodItem(0, "", "", "", "", 0, "", 0, 0, 0, 0, null);
            } else if (localName.equals("productcode_ean")) {
                this.in_productcode_ean = true;
            } else if (localName.equals("id")) {
                this.in_id = true;
            } else if (localName.equals("amount")) {
                this.in_amount = true;
            } else if (localName.equals("amount_measuring_system")) {
                this.in_amount_measuring_system = true;
            } else if (localName.equals("kcal")) {
                this.in_kcal = true;
            } else if (localName.equals("protein_gram")) {
                this.in_protein_gram = true;
            } else if (localName.equals("kh_gram")) {
                this.in_kh_gram = true;
            } else if (localName.equals("fat_gram")) {
                this.in_fat_gram = true;
            } else if (localName.equals("servings")) {
                this.in_servings = true;
                this.servings = new HashMap<String, Double>();
            } else if (localName.equals("serving")) {
                this.in_serving = true;
            } else if (localName.equals("name")) {
                // there are two <name>-Tags, one for the item itself and one for the servings
                if (this.in_serving) {
                    this.in_serving_name = true;
                } else {
                    this.in_fooditem_name = true;
                }
            } else if (localName.equals("weight_gram")) {        
                this.in_serving_amount = true;
            } else if (localName.equals("description")) {
                this.in_description = true;
            } else if (localName.equals("producer")) {
                this.in_fooditem_producer = true;
            } else if (localName.equals("option")) {
                this.in_fooditem_option = true;
            }
            
        }

        /**
         * Gets be called on closing tags like: </tag>
         */
        @Override
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
            
            text = textContent.toString();
            text = convertSpecialCharacter(text);
            
            if (localName.equals("item")) {
                this.in_item = false;
                foodItems.add(foodItem);
            } else if (localName.equals("productcode_ean")) {
                this.in_productcode_ean = false;
                foodItem.setProductCodeEan(text);
            } else if (localName.equals("id")) {
                foodItem.setId(Integer.parseInt(text));
                this.in_id = false;
            } else if (localName.equals("amount")) {
                this.in_amount = false;
                foodItem.setCalculationBaseAmount(Integer.parseInt(text));
            } else if (localName.equals("amount_measuring_system")) {
                this.in_amount_measuring_system = false;
                foodItem.setCalculationBaseUnit(text);
            } else if (localName.equals("kcal")) {
                this.in_kcal = false;
                foodItem.setKilokalories(Double.parseDouble(text));
            } else if (localName.equals("protein_gram")) {
                this.in_protein_gram = false;
                foodItem.setProteinAmount(Double.parseDouble(text));
            } else if (localName.equals("kh_gram")) {
                this.in_kh_gram = false;
                foodItem.setCarbohydratesAmount(Double.parseDouble(text));
            } else if (localName.equals("fat_gram")) {
                this.in_fat_gram = false;
                foodItem.setFatAmount(Double.parseDouble(text));
            } else if (localName.equals("servings")) {
                this.in_servings = false;
                this.foodItem.setServings(servings);
            } else if (localName.equals("weight_gram")) {        
                    this.in_serving_amount = false;
                    this.serving_amount = Double.parseDouble(text);
            } else if (localName.equals("serving")) {
                servings.put(serving_name, serving_amount);
                this.in_serving = false;
            } else if (localName.equals("name")) {
                // there are two <name>-Tags, one for the item itself and one for the servings
                if (this.in_serving) {
                    this.serving_name = text;
                    this.in_serving_name = false;
                } else {
                    this.in_fooditem_name = false;
                    foodItem.setName(text);
                }
            } else if (localName.equals("weight_gram")) {        
                this.in_serving_amount = false;
            } else if (localName.equals("description")) {
                this.in_description = false;
            } else if (localName.equals("producer")) {
                this.in_fooditem_producer = false;
                foodItem.setProducer(text);
            } else if (localName.equals("option")) {
                this.in_fooditem_option = false;
                foodItem.setOption(text);
            }
            
        }

        /**
         * Gets be called on the following structure: <tag>characters</tag>
         */
        @Override
        public void characters(char ch[], int start, int length) {
            
            textContent.append(ch, start, length);
            
        }
}
 
Kennt latin 1 überhaupt solche umlaute? Oo?

LG. Dagobert

Gesendet von meinem GT-I9300 mit der Android-Hilfe.de App
 
Nach kurzem googlen scheint es so, dass Latin 1 diese Sonderzeichen unterstützt. Die Sonderzeichen sind ja auch in den Objekten enthalten.

Wenn ich mir die Objekte per System.out ausgeben lasse, dann werden auch hier die Umlaute "falsch" dargestellt.
Also zum Beispiel "W&uuml;rfelzucker" anstelle von Würfelzucker.
 
Naja ich würde meiner kurzen googlelei und dem vertrauen von Wikipedia, sagen das es kein Latin 1 Standard ist, sondern eine HTML Codierung.

lg. Dagobert
 
Was aber wäre denn ein LÖSUNGSansatz :)
 
&uuml; ist sicher html - steht für ü
&ouml; wäre ein ö usw

eine mögliche lösung wäre Html.fromHTML zu nutzen
 
Meine Antwort sollte meinen, dass Latin 1 "ü, ö, ä" etc. kennt.
&uuml; ist natürlich HTML.

Jedenfalls funktioniert deine Lösung prima! Vielen Dank!
 
Zurück
Oben Unten