SAX parsing progblem in android
Hi i have an android application and i want to get a data from xml file. i have use SAX parser but there is some problem to get data from this type of xml file given here, so please given me the solution for parse the following xml file using SAX parsing
my xml file is here
<?xml version="1.0" encoding="utf-8"?>
<xml>
<movie>
<file>
<type>1</type>
<url>http://www.mauitheatre.com/</url>
<path>http://64.250.238.26:1111/clips/UlalenaSplashAdd.jpg</path>
<title>UlalenaSplash</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/BaldwinBeach.mp4</path>
<title>Baldwin Beach</title>
</file>
</movie>
<movie>
<file>
<type>0</type>
<url></url>
<path>http://64.250.238.26:1111/clips/AppTeaser.mp4</path>
<title>SlackKeyShow</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/BigBeach.mp4</path>
<title>Big Beach</title>
</file>
</movie>
<movie>
<file>
<type>1</type>
<url>http://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea</url>
<path>http://64.250.238.26:1111/clips/raiateaADD.jpg</path>
<title>Raiatea Keola Beamer add</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/CharleyYoungBeach.mp4</path>
<title>Charley Young Beach</title>
</file>
</movie>
<movie>
<file>
<type>1</type>
<url>http://www.bennyuyetake.com</url>
<path>http://64.250.238.26:1111/clips/BennyUyetake.jpg</path>
<title>Benny Uyetake SPlash</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/HamoaBeach-1.mp4</path>
<title>Hamoa Beach</title>
</file>
</movie>
<movie>
<file>
<type>1</type>
<url>http://www.dericksebastian.com</url>
<path>http://64.250.238.26:1111/clips/DSSplash.jpg&开发者_运维百科lt;/path>
<title>DS Splash</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/HanaBay.mp4</path>
<title>Hana Bay</title>
</file>
</movie>
<movie>
<file>
<type>1</type>
<url>http://www.mountainapplecompany.com/new-releases/keola-beamer-and-raiatea</url>
<path>http://64.250.238.26:1111/clips/raiateaADD.jpg</path>
<title>Raiatea Keola Beamer add</title>
</file>
<file>
<type>0</type>
<path>http://64.250.238.26:1111/clips/KamaoleBeachPark1b-1.mp4</path>
<title>Kamaole Beach Park 1</title>
</file>
</movie>
</xml>
Assuming that you need an ArrayList<Movie>
, where the structure of the Movie
class is
public class Movie
{
private ArrayList<File> files;
public Movie()
{
this.files = new ArrayList<File>();
}
}
and the File
's structure is
public class File
{
private int type;
private String url;
private String path;
private String title;
}
with the necessary getter
and setter
functions, you can get the desired list by
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(new InputSource(new StringReader(yourXmlString)), handler);
final ArrayList<Movie> movies = handler.getRecords();
where
yourXmlString
is the xml data you've pasted above, andhandler
is a MovieXmlHandler instance.
MovieXmlHandler implementation:
public class MovieXmlHandler extends DefaultHandler
{
private static final String TAG_MOVIE = "movie";
private static final String TAG_FILE = "file";
private static final String TAG_TYPE = "type";
private static final String TAG_URL = "url";
private static final String TAG_PATH = "path";
private static final String TAG_TITLE = "title";
private String currentNodeName;
private Movie currentMovie;
private File currentFile;
private ArrayList<Movie> records = null;
private String elementValue;
public ArrayList<Movie> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<Movie>();
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes att) throws SAXException
{
if (localName != null)
currentNodeName = localName;
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_MOVIE))
this.currentMovie = new Movie();
if (this.currentNodeName.equalsIgnoreCase(TAG_FILE))
this.currentFile = new File();
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
this.currentFile.setType(Integer.parseInt(this.elementValue));
else if (this.currentNodeName.equalsIgnoreCase(TAG_URL))
this.currentFile.setUrl(this.elementValue);
else if (this.currentNodeName.equalsIgnoreCase(TAG_PATH))
this.currentFile.setPath(this.elementValue);
else if (this.currentNodeName.equalsIgnoreCase(TAG_TITLE))
this.currentFile.setTitle(this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_MOVIE))
{
if (this.currentMovie != null)
this.records.add(this.currentMovie);
}
else if (localName.equalsIgnoreCase(TAG_FILE))
{
if ((this.currentMovie != null) && (this.currentFile != null))
this.currentMovie.getFiles().add(this.currentFile);
}
currentNodeName = null;
}
}
Of course, if you have only the url of your xml data (xmlUrl:String
), you can use
final URL sourceUrl = new URL(xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final MovieXmlHandler handler = new MovieXmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
final ArrayList<Movie> movies = handler.getRecords();
and if you have your xml data available from an in:InputStream
, then
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final MovieXmlHandler handler = new MovieXmlHandler();
parser.parse(in, handler);
final ArrayList<Movie> movies = handler.getRecords();
Let us know if this isn't what you were looking for.
The real problem is that you are trying to use a SAX parser yourself; which makes life harder than it needs to be. If possible, why not rely on an Annotation based XML parsing framework like the Simple XML Library?
I just wrote a blog post on how to include it in your Android projects.
The problem is probably that the <xml>
tag is not closed. To be able to parse the file, it must be a well-formed XML file, so try to add </xml>
at the end and give it another try.
精彩评论