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!!