Hi

I've written a simple class that should create objects from a given
XML file. This works fine when I run it on desktop but when I run the
exact same code on android it doesn't find anything...

It doesn't cast any exceptions but at the end area is still null and
boulders an empty list. The problem is that I don't know how to start
analyzing the problem...

I'm pasting my parser code and the content of a file that I know don't
work (when run on Android 1.6 on a HTC Tatto)


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;


public class XMLParser implements ContentHandler {
        private XMLReader reader;
        private String name;
        private String description;
        private String boulderName;
        private String boulderGrade;
        private String boulderDescription;
        private float longi, lati;
        private boolean area = false;
        private boolean boulder = false;
        private boolean path = false;
        private List<Boulder> boulders;

        public XMLParser() {
                try{
                        SAXParserFactory spf = SAXParserFactory.newInstance();
                        reader = spf.newSAXParser().getXMLReader();
                        reader.setContentHandler(this);
                }
                catch(Exception e) {

                }
        }

        public void parse(File input) throws IOException, SAXException {
                InputStream stream = new FileInputStream(input);

                reader.parse(new InputSource(stream));
        }

        @Override
        public void characters(char[] ch, int start, int length)
                        throws SAXException {
                String text = (new String(ch, start, length)).trim();

                if( path ) {
                        parsePath(text);
                }
                else if( boulder ) {
                        Scanner scanner = new Scanner(new String(ch, start, 
length));
                        parseBoulder(scanner);
                }
                else if( area ) {
                        parseArea(text);
                }
                else {
                        throw new SAXException("Unable to parse, unknown 
state.");
                }
        }

        // TODO: rewrite, is completely without grace
        private void parseArea(String text) {
                Scanner scanner = new Scanner( text );

                String name = scanner.findInLine("name\\s*=\\s*\".+\"");
        if( name != null ) {
            name = name.substring(name.indexOf('"')+1,
name.length()-1);
            this.name = name;
        }

        String description = scanner.findInLine("description\\s*=\\s*
\".+\"");
        if( description == null ) {
                description = scanner.findInLine("description\\s*=\\s*\".+");

                if( description != null ) {
                        scanner.nextLine();
                        String line = null;
                        while( scanner.hasNext() && !(line =
scanner.nextLine()).contains("\"") ) {
                                 description += "\n" + line.trim();
                        }
                        if( line != null ) {
                                description += "\n" + line.substring(0, 
line.indexOf('"')
+1).trim();
                        }
                }
        }
        if( description != null ) {
                description = description.substring(description.indexOf('"')
+1, description.length()-1);
                this.description = description;
        }
        }

        private void parseBoulder(Scanner scanner) throws SAXException {

                String name = scanner.findInLine("name\\s*=\\s*\".+\"");
        if( name != null ) {
            name = name.substring(name.indexOf('"')+1,
name.length()-1);
            boulderName = name;
        }

        String grade = scanner.findInLine("grade\\s*=\\s*\".+\"");
        if( grade != null ) {
            grade = grade.substring(grade.indexOf('"')+1,
grade.length()-1);
            boulderGrade = grade;
        }

        String longi = scanner.findInLine("longitud\\s*=\\s*\".+\"");
        if( longi != null ) {
            longi = longi.substring(longi.indexOf('"')+1,
longi.length()-1);
            try{
                this.longi = Float.parseFloat(longi);
            }
            catch( Exception e ) {
                throw new SAXException("Error: Unable to parse
longitude.");
            }
        }

        String lati = scanner.findInLine("latitud\\s*=\\s*\".+\"");
        if( lati != null ) {
            lati = lati.substring(lati.indexOf('"')+1,
lati.length()-1);
            try{
                this.lati = Float.parseFloat(lati);
            }
            catch( Exception e ) {
                throw new SAXException("Error: Unable to parse
latitude.");
            }
        }

        String description = scanner.findInLine("description\\s*=\\s*
\".+\"");
        if( description == null ) {
                description = scanner.findInLine("description\\s*=\\s*\".+");

                if( description != null ) {
                        scanner.nextLine();
                        String line = null;
                        while( scanner.hasNext() && !(line =
scanner.nextLine()).contains("\"") ) {
                                 description += "\n" + line.trim();
                        }
                        if( line != null ) {
                                description += "\n" + line.substring(0, 
line.indexOf('"')
+1).trim();
                        }
                }
        }
        if( description != null ) {
                description = description.substring(description.indexOf('"')
+1, description.length()-1);
                boulderDescription = description;
        }

        if( scanner.hasNextLine() )  {
                scanner.nextLine();
                parseBoulder( scanner );
        }
        }

        private void parsePath(String text) {   }

        @Override
        public void endDocument() throws SAXException {    }

        @Override
        public void endElement(String uri, String localName, String qName)
                        throws SAXException {

                if(qName.equals("area")) {
                        area = false;
                }
                else if(qName.equals("boulder")) {
                        boulder = false;
                        boulders.add(new Boulder(longi, lati, boulderName,
boulderDescription,
                                        
Boulder.Grade.string2Grade(boulderGrade), boulders.size()+1));
                }
                else if(qName.equals("path")) {
                        path = false;
                }

        }

        @Override
        public void endPrefixMapping(String prefix) throws SAXException
{    }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length)
                        throws SAXException {    }

        @Override
        public void processingInstruction(String target, String data)
                        throws SAXException {    }

        @Override
        public void setDocumentLocator(Locator locator) {    }

        @Override
        public void skippedEntity(String name) throws SAXException {    }

        @Override
        public void startDocument() throws SAXException {

                name = null;
                description = null;
                area = false;
                boulder = false;
                path = false;
                boulders = new ArrayList<Boulder>();
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                        Attributes atts) throws SAXException {

                if(qName.equals("area")) {
                        area = true;
                }
                else if(qName.equals("boulder")) {
                        boulder = true;
                }
                else if(qName.equals("path")) {
                        path = true;
                }
                else {
                        System.err.println("Unknown element: " + qName);
                }

        }

        @Override
        public void startPrefixMapping(String prefix, String uri)
                        throws SAXException {    }

        public List<Boulder> getBoulders() {
                return new ArrayList<Boulder>(boulders);
        }

        public String getAreaName() {
                return name;
        }

        public String getAreaDescription() {
                return description;
        }
}


File that I tried to parse


<?xml version="1.0"?>
<area>
   name = "lägenhet"
   <boulder>
      name = "fönster"
      grade = "5"
      longitud = "12.030479"
      latitud="57.73776"
      description = "det högra"
   </boulder>
   <boulder>
      name = "2"
      grade = "3"
      longitud = "12.030657"
      latitud="57.73799"
      description = ""
   </boulder>
</area>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to