1. Are you ready for the Galaxy S20? Here is everything we know so far!

Loading XML Files into Android App

Discussion in 'Android Apps & Games' started by escobarceasar, Feb 11, 2011.

  1. escobarceasar

    escobarceasar Lurker
    Thread Starter

    Hi people,

    I'm writing an android app that loads xml files from the /data/data/{appname}/files directory, parses the files using SAX and puts the contents of the xml files into Java objects.

    I've managed to read in the xml files successfully using this code:
    FileInputStream fis = activity.openFileInput(fileName);

    where "fileName" is the name of a file in the /data/data/{appname}/files directory.

    I've also got the parsing working to some extent using Xml.parse(...)

    THE PROBLEM is, when the tags are read in, some of them return with the expected content and some return with a "null" as if there is no content in there. I *suspect* the problem has to do with not escaping special characters such as full stops, tags, spaces, html text etc but I'm not sure. I have never had this problem is regular Java development - usually an error is thrown stating why the content was rejected by the parser.

    Here is the xml file I' trying to read in/parse:
    <?xml version="1.0" encoding="utf-8"?>

    <author>S. Odeyemi </author>
    <version>0.1 </version>

    <title><b>Java 101</b></title>

    <introduction>hi people</introduction>

    <section>section 1</section>
    <section>section 2</section>
    <section>section 3</section>
    <section>section 4</section>



    All the data in the "section" tags were read in successfully, however, the "introduction" and "author" tags returned a empty string. Why is that? Also, "version" tag returned null.

    Here's the SAX Handler code if it helps diagnose the problem:
    public class TutXMLHandler extends DefaultHandler{
    private List<Presentation> presentations;
    private Presentation currentPresentation;
    private StringBuilder builder;

    public List<Presentation> getPresentations(){
    return this.presentations;
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    super.characters(ch, start, length);
    builder.append(ch, start, length);

    public void startDocument() throws SAXException {
    presentations = new ArrayList<Presentation>();
    builder = new StringBuilder();

    public void startElement(String uri, String localName, String name,
    Attributes attributes) throws SAXException {
    super.startElement(uri, localName, name, attributes);
    if (localName.equalsIgnoreCase(BasePresentationParser.PRESENTATION)){
    //create a presentation object as soon as we
    //detect a presentation tag in the xml.
    this.currentPresentation = new Presentation();
    if (localName.equalsIgnoreCase(BasePresentationParser.SECTION) & this.currentPresentation.getSections() == null){
    //create a section List object and add to presentation as soon as we
    //detect the first section tag in the xml.
    this.currentPresentation.setSections(new ArrayList<String>());

    public void endElement(String uri, String localName, String name)
    throws SAXException {
    super.endElement(uri, localName, name);
    if (this.currentPresentation != null){
    //TODO: more attributes still need to be added
    if (localName.equalsIgnoreCase(BasePresentationParser.TITLE)){
    } else if (localName.equalsIgnoreCase(BasePresentationParser.LINK)){
    else if (localName.equalsIgnoreCase(BasePresentationParser.INTRODUCTION)){
    else if (localName.equalsIgnoreCase(BasePresentationParser.SUB_TITLE)){
    } else if (localName.equalsIgnoreCase(BasePresentationParser.AUTHOR)){
    } else if (localName.equalsIgnoreCase(BasePresentationParser.SECTION)){
    } else if (localName.equalsIgnoreCase(BasePresentationParser.PRESENTATION)){


    Thanks in advance for all help!!

    1. Download the Forums for Android™ app!



Share This Page