Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading XML Files into Android App

 
Siju Odeyemi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?>
<presentation>

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

<title>Java 101</title>

<introduction>hi people</introduction>

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

<link></link>
<image></image>

</presentation>


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;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
builder.append(ch, start, length);
}


@Override
public void startDocument() throws SAXException {
super.startDocument();
presentations = new ArrayList<Presentation>();
builder = new StringBuilder();
}


@Override
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>());
}
}

@Override
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)){
currentPresentation.setTitle(builder.toString());
} else if (localName.equalsIgnoreCase(BasePresentationParser .LINK)){
currentPresentation.setLink(builder.toString());
}
else if (localName.equalsIgnoreCase(BasePresentationParser .INTRODUCTION)){
currentPresentation.setIntroduction(builder.toStri ng());
}
else if (localName.equalsIgnoreCase(BasePresentationParser .SUB_TITLE)){
currentPresentation.setSubTitle(builder.toString() );
} else if (localName.equalsIgnoreCase(BasePresentationParser .AUTHOR)){
currentPresentation.setAuthor(builder.toString());
} else if (localName.equalsIgnoreCase(BasePresentationParser .SECTION)){
currentPresentation.getSections().add(builder.toSt ring());
currentPresentation.setAuthor(builder.toString());
} else if (localName.equalsIgnoreCase(BasePresentationParser .PRESENTATION)){
presentations.add(currentPresentation);
}
builder.setLength(0);
}
}

}

Thanks in advance for all help!!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "html text"? The XML file doesn't seem to contain any HTML. If it in fact does (any you just didn't post the whole thing) then it would need to be kept in a CDATA section.

My first guess would be that some of the BasePresentationParser.XYZ strings are misspelt.

Does that handler work for that file in a Java SE environment? Would be easier to debug than as part of an Android app.

In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will bother to do so.
 
Siju Odeyemi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:What do you mean by "html text"? The XML file doesn't seem to contain any HTML. If it in fact does (any you just didn't post the whole thing) then it would need to be kept in a CDATA section.

My first guess would be that some of the BasePresentationParser.XYZ strings are misspelt.

Does that handler work for that file in a Java SE environment? Would be easier to debug than as part of an Android app.

In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will bother to do so.


Thanks for the response Ulf. Indeed I posted the wrong xml. The one I intended to post is slightly different and has some of the text wrapped in bold and italic (html) tags.

As you've stated, my problem was probably caused by not using CDATA section to store the html content. Could you please specify an example of how the CDATA section should look? In Adobe Flex I would do something like this but I'm not sure if it's Flex specific:




 
Siju Odeyemi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf, the CDATA section solves my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic